第二季-面向对象基础学习
1.1面向对象简介知识点
1.2 程序代码
namespace 类1
{
class Program
{
static void Main(string[] args)
{
person p1 = new person();
p1.age = 15;
p1.sex = "男";
p1.sayHellow();
Console.ReadKey();
}
}
class person
{
public int age;
public string sex;
public string name;
public void sayHellow()
{
Console.WriteLine("你好,我叫小明");
}
}
}
程序代码
namespace 类1
{
class Program
{
static void Main(string[] args)
{
person p1 = new person();
p1.age = 15;
p1.sex = "男";
p1.name = "Tom";
p1.sayHellow();
Console.ReadKey();
}
}
class person
{
public int age;
public string sex;
public string name;
public void sayHellow()
{
Console.WriteLine("大叫好,我叫"+this.name+",性别"+this.sex+",今年"+this.age+"岁了.");
// Console.WriteLine("大叫好,我叫{0},性别{1},今年{2}岁了。",this.name,this.sex,this.age);
//这两种方式都可以
}
}
}
2.1 成员访问级别知识点
2.2 程序代码
namespace 类1
{
class Program
{
static void Main(string[] args)
{
person2 p1 = new person2();
//p1.age = 15;//出现错误,不可以访问privite字段。
//p1.sex = "男";
//p1.name = "Tom";
//p1.sayHellow();
Console.ReadKey();
}
}
class person2
{
private int age;
private string sex;
private string name;
public void sayHellow()
{
Console.WriteLine("大叫好,我叫"+this.name+",性别"+sex+",今年"+this.age+"岁了.");
//privite在自己类中可以访问,出了类就不可以访问。字段和方法一样。
}
}
}
如果需要访问,不妨这样设置。(所有的字段都不要写private)
namespace 类1
{
class Program
{
static void Main(string[] args)
{
person2 p1 = new person2();
//p1.age = 15;
//p1.sex = "男";
//p1.name = "Tom";
//p1.sayHellow();
p1.giveName("Tom");//这种方式可以访问其他的类中private字段和方法。
Console.ReadKey();
}
}
class person2
{
private int Age;
private string Sex;
private string Name;
private void sayHellow()
{
Console.WriteLine("大叫好,我叫"+this.Name + ",性别"+ Sex + ",今年"+this.Age + "岁了.");
}
public void giveName(string name)
{
if ( name = "jeery")
{
return;//这样的好处可以具有选择性。
}
this.Name = name;
}
}
}
3.2 属性知识点
3.2 程序代码
namespace 类1
{
class Program
{
static void Main(string[] args)
{
person3 p3 = new person3();
p3.Age = 15;
Console.WriteLine("{0}", p3.Age);
Console.ReadKey();
}
}
class person3
{
private int age;
public int Age
{
set
{
if( value < 0)
{
return;
}
this.age = value;//属性和字段的区别:使用属性,优点可以控制非法值
}
get
{
return this.age;
}
}
}
}
程序代码
namespace 类1
{
class Program
{
static void Main(string[] args)
{
person3 p3 = new person3();
p3.Age = 15;
p3.Age = p3.Age + 1;
Console.WriteLine("{0}", p3.Age);
Console.ReadKey();
}
}
class person3
{
public int Age
{
set
{
}
get
{
return 3;//最终赋值为3
}
}
}
}
运行结果
4.1 知识点
4.2 只读属性知识点
namespace 类1
{
class Program
{
static void Main(string[] args)
{
person3 p3 = new person3();
//p3.Age = 30;//只读属性无法赋值
p3.IncAge();
p3.IncAge();
Console.WriteLine("{0}",p3.Age);
Console.ReadKey();
}
}
class person3
{
private int age;
public int Age
{
get{ return this.age;}//只读属性(比较常见),只写属性比较少
}
public void IncAge()
{
age++;
}
}
}
简单属性
namespace 类1
{
class Program
{
static void Main(string[] args)
{
person3 p3 = new person3();
p3.Age = 30;//只读属性无法赋值
Console.WriteLine("{0}",p3.Age);
Console.ReadKey();
}
}
class person3
{
public int Age
{
get;
set;
}
}
}
5.1 对象的引用知识点
5.2 程序代码
6.1 构造函数
6.2 程序代码
namespace 类1
{
class Program
{
static void Main(string[] args)
{
person3 p1 = new person3("Tom");
Console.ReadKey();
}
}
class person3
{
public person3(string name)//构造函数
{
}
}
}
namespace 类1
{
class Program
{
static void Main(string[] args)
{
person3 p1 = new person3();
Console.ReadKey();
}
}
class person3
{
public person3()//不写时,默认没有参数的构造函数
{
}
}
}
构造函数的作用,在创建对象并且在构造函数对对象初始化。
构造函数可以重载(两个构造函数,参数不同)。
//好处:提高了封装性能
构造函数常用写法
namespace 类1
{
class Program
{
static void Main(string[] args)
{
person3 p1 = new person3();
Console.ReadKey();
}
}
class person3
{
public int Age { get; set; }
public string Name { get; set; }
public person3()
{
this.Age = 0;
this.Name = "未命名";
}
public person3(string name)
{
this.Name = name;
}
public person3(string name, int age)
{
this.Name = name;
this.Age = age;
}
}
}
7.1 继承知识点
7.2 程序代码
namespace 继承1
{
class Program
{
static void Main(string[] args)
{
Chinese p1 = new Chinese();
Japanese p2=new Japanese();
p1.Age = 20;
p1.Name = "中国人";
p1.sayHellow();//调用父类
p1.gongFu();//调用自己的方法
Console.ReadKey();
}
}
class Person//父类
{
public string Name { get; set; }
public int Age { get; set; }
public void sayHellow()
{
Console.WriteLine("大家好");
}
}
class Chinese:Person//子类
{
public void gongFu()//自己的函数
{
Console.WriteLine("我打!");
}
}
class Japanese:Person//第二子类
{
public void eatFood()//自己的函数
{
Console.WriteLine("会吃");
}
}
}
在继承过程中常见的错误。(Object类是所有类的父类,没有指定父类默认是Object类),所有的变量类型都可以指向Object类。
class Program
{
static void Main(string[] args)
{
Chinese p1 = new Chinese();
Japanese p2 = new Japanese();
//合理,我要一个人,给了我一个中国人
Person p3 = p1;
p3.sayHellow();
//合理,我要一个中国人,你给我一个人
Chinese p6 =(Chinese) p3;//必须进行强制转换
p3.sayHellow();
//不合理,我要一个中国人,给了我一个人;
//Chinese p4 = (Chinese)new Person();//无法强制转换
//p4.sayHellow();
//不合理,我要一个中国人,你给了一个韩国人
Chinese p5 = (Chinese) p2;
p1.Age = 20;
p1.Name = "中国人";
p1.sayHellow();
p1.gongFu();
Console.ReadKey();
}
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public void sayHellow()
{
Console.WriteLine("大家好");
}
}
class Chinese:Person
{
public void gongFu()
{
Console.WriteLine("我打!");
}
}
class Japanese : Person//第二子类
{
public void eatFood()//自己的函数
{
Console.WriteLine("会吃");
}
}
}
8.1 异常知识点
8.2 程序代码
namespace 异常1
{
class Program
{
static void Main(string[] args)
{
try
{
int i = Convert.ToInt32("abc");
}
catch(Exception ex)
{
Console.WriteLine("出现数据转换错误");
}
Console.ReadKey();
}
}
}
namespace 异常2
{
class Program
{
static void Main(string[] args)
{
try
{
string getAge = getAgeDsce(100);
}
catch(Exception ex)
{
Console.WriteLine("错误");
}
Console.ReadKey();
}
static string getAgeDsce(int age)
{
if (age > 0 && age < 10)
return "小孩";
else if (age >= 10)
return "大人";
else
throw new Exception("不可能");
}
}
}
9.1 常量与静态成员
9.2 程序代码
namespace 常量1
{
class Program
{
public const double e = 2.71;//全局常量,不用new一个类直接用
static void Main(string[] args)
{
const double pi = 0.3;//局部常量,不可以添加Public
int r = 3;
double S = pi * r * r;
Console.WriteLine("面积为{0}", S);
person p1 = new person();
Console.WriteLine(p1.S1);
Console.ReadKey();
}
}
class person
{
public double S1 = Program.e;
//double S2 = Program.pi;//也是不可以使用的
}
}
namespace 静态变量和方法1
{
class Program
{
static void Main(string[] args)
{
Person.S1 = 30;//不需要New使用的叫静态成员
Person.Talk();//也可以不需要New使用静态方法
Console.WriteLine(Person.S1);
Dog dog = new Dog();
dog.jog();//
Console.ReadKey();
}
}
public class Person
{
public static double S1;//静态变量
public static void Talk()//静态方法可以使用
{
Console.WriteLine("说话{0}", S1);
}
}
public class Dog
{
public void jog()
{
Console.WriteLine("叫唤{0}",Person.S1);//静态成员也可以在其他类直接使用,不需要New;
}
}
}
程序代码易错点
namespace 静态成员1
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Person();
p1.Count();
Console.ReadKey();
}
}
public class Person
{
public static double S1;//静态变量
public int Age;
public static void Talk()//静态方法可以使用
{
// Console.WriteLine("说话{0}+{1}", S1,Age);//报错,在static中不可以使用非static成员
}
public void Count()
{
Console.WriteLine("说话{0}+{1}", S1, Age);//静态成员可以在非静态方法中使用
}
}
}
class Program
{
static void Main(string[] args)
{
int p=consoleHelper.changeData();
Console.WriteLine("{0}",p);//静态类直接调用
//consoleHelper control = new consoleHelper();//报错,静态类不能实例化,一般用来实现一些函数库
Console.ReadKey();
}
}
static class consoleHelper//静态类,不能被实例化
{
public static int changeData()
{
string s = Console.ReadLine();
return Convert.ToInt32(s);
}
}
10.1 namespace命名空间知识点
通过文件夹,可以允许文件重名,两个类位于不同的命名空间下,可以解决此问题
10.2 程序代码
方法一:
namespace 类1
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Person();
类1.hr. Person p2 = new 类1.hr.Person();//调用相同的类,处在不同的位置 方法1的解决
p2.Dog();//调用hr下的方法
Console.ReadKey();
}
}
class Person
{
}
}
//方法二
using 类1.hr;//添加引用,位于hr下的类都可以引用
11.1 索引器知识点
参数不止一个,类型也不一定固定;索引也可以重载。
11.2 程序代码
namespace 类1
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Person();
p1[1] = "小华";
Console.WriteLine(p1[1] + p1[2]);
Console.ReadKey();
}
}
class Person
{
private string firstName = "大娃";
private string secondName = "二娃";
public string this[int index]
{
get
{
if (index == 1)
{
return firstName;
}
else if (index == 2)
{
return secondName;
}
else
{
throw new Exception("数据错误");
}
}
set
{
if(index==1)
{
firstName = value;
}
else if(index==2)
{
secondName = value;
}
else
{
throw new Exception("数据错误");
}
}
}
}
}