Charater 6
6.16 this关键字
class Program
{
static void Main(string[] args)
{
var compare = new Compare();
Console.WriteLine(compare.compareValue(10));
}
}
class Compare
{
int myValue = 10;
public int compareValue(int myValue)
{
return myValue > this.myValue ? myValue : this.myValue;
// 参数 字段 参数 字段
}
}
6.17 索引器 indexer[]
class Program
{
static void Main(string[] args)
{
var employee = new Employee();
employee[0] = "Hongyong";
employee[1] = "Zhao";
employee[2] = "Suzhou";
Console.WriteLine("{0} {1} lives in {2}.",employee[0],employee[1],employee[2]);
}
}
class Employee
{
public string lastName;
public string firstName;
public string cityOfBirth;
public string this[int index] //indexer格式:修饰符+返回类型+this[int index]
{
get
{
switch (index)
{
case 0:return lastName;
case 1:return firstName;
case 2:return cityOfBirth;
default:
throw new ArgumentOutOfRangeException("index");
}
}
set
{
switch (index)
{
case 0: lastName = value;
break;
case 1:firstName = value;
break;
case 2:cityOfBirth = value;
break;
default:
throw new ArgumentOutOfRangeException("index");
}
}
}
}
class Program
{
static void Main(string[] args)
{
var empy1 = new Employee();
Console.WriteLine("{0} {1}",empy1[0],empy1[1]);//默认值为0
empy1[0] = 123;
empy1[1] = 234;
Console.WriteLine("{0} {1}", empy1[0], empy1[1]);
}
}
class Employee
{
int temp0;
int temp1;
public int this[int index]
{
get { return (0 == index) ? temp0 : temp1; }
set
{
if (0 == index)
{
temp0 = value;
}
else
temp1 = value;
}
}
}
6.18 访问器的访问修饰符 public protected private
class Program
{
static void Main(string[] args)
{
var albert = new Albert();
string a = albert.Person("zhaohongyong");
Console.WriteLine(a);
}
}
class Albert
{
public string Name { get; private set; } //只允许在类中进行设置属性
public string Person(string name)
{
Name = name;
return Name;
}
}
6.20 分部方法 partial
class Program
{
static void Main(string[] args)
{
var alert = new Alert();
alert.Add(3, 5);
}
}
partial class Alert
{
partial void PrintSum(int x, int y);//定义分部方法
public void Add(int x,int y)
{
PrintSum(x, y);
}
}
partial class Alert
{
partial void PrintSum(int x, int y) //实现部分
{
Console.WriteLine("Sum is {0}",x+y);
}
}
class Program
{
static void Main(string[] args)
{
var alert = new Alert();
alert.Calc(3, 5);
}
}
partial class Alert
{
partial void PrintCalc(int x, int y);
public void Calc(int x ,int y)
{
PrintCalc(x, y);
}
}
partial class Alert
{
partial void PrintCalc(int x, int y)
{
Console.WriteLine(x + y);
}
}
Charater 7
7.1 类继承 冒号
class Program
{
static void Main(string[] args)
{
var calc = new CalcInter();
calc.x = 8;
calc.y = 2;
Console.WriteLine(calc.CalcDiv(calc.x, calc.y));
}
}
class Calc
{
public int x, y;
public double CalcAdd(int x,int y)
{
return x + y;
}
}
class CalcInter:Calc
{
public int field1, field2;
public double CalcDiv(int x,int y)
{
return x / y;
}
}
7.4 屏蔽基类的成员 new
class Program
{
static void Main(string[] args)
{
var someClass = new SomeClass();
someClass.Method1("someclass test");
Console.WriteLine(someClass.field1 );
var otherClass = new OtherClass();
otherClass.Method1("otherclass test");
Console.WriteLine(otherClass.field1);
}
}
class SomeClass
{
public string field1 = "SomeClass field1";
public void Method1(string value)
{
Console.WriteLine("SomeClass.Method1 :{0}",value);
}
}
class OtherClass : SomeClass
{
new public string field1 = "SomeClass field2";//屏蔽数据成员 new 相同类型+相同名称
new public void Method1(string value) //屏蔽函数成员 new 相同的签名,签名=名称+参数列表,不包括返回类型
{
Console.WriteLine("OtherClass.Method1:{0}",value);
}
}
7.5 基类访问 base
class Program
{
static void Main(string[] args)
{
var otherClass = new OtherClass();
string other = otherClass.Field1; //派生类中字段
Console.WriteLine( other); //派生类中的字段
otherClass.PrintField1();
}
}
class SomeClass
{
public string Field1 = "Field1--In the base class";//基类数据成员
}
class OtherClass : SomeClass
{
new public string Field1 = "Field1-- In the derived class";//屏蔽基类数据成员
public void PrintField1()
{
Console.WriteLine(Field1); //继承类中Fiels1
Console.WriteLine(base.Field1); //基类访问符base
}
}
7.6 使用基类引用
class Program
{
static void Main(string[] args)
{
var derivedClass = new Myderived();
derivedClass.Print();
var mybc = (MyBaseClass)derivedClass;//将derivedClass变量赋值给mybc,类型转换为基类类型,可见范围缩小。
mybc.Print();
}
}
class MyBaseClass
{
public void Print()
{
Console.WriteLine("This is the base class");
}
}
class Myderived:MyBaseClass
{
new public void Print()
{
Console.WriteLine("This is the derived class");
}
}
class Program
{
static void Main(string[] args)
{
var derivedClass = new Myderived();
derivedClass.Print();
var mybc = (MyBaseClass)derivedClass;
mybc.Print();
}
}
class MyBaseClass
{
virtual public void Print()
{
Console.WriteLine("This is the base class");
}
}
class Myderived:MyBaseClass
{
override public void Print()
{
Console.WriteLine("This is the derived class");
}
}
class Program
{
static void Main(string[] args)
{
var derivedClass = new Myderived();
var secondDerived = new SecondDerived();
derivedClass.Print();
var mybc = (MyBaseClass)secondDerived;
mybc.Print();
}
}
class MyBaseClass
{
virtual public void Print()
{
Console.WriteLine("This is the base class");
}
}
class Myderived:MyBaseClass
{
override public void Print()
{
Console.WriteLine("This is the derived class");
}
}
class SecondDerived:Myderived
{
override public void Print()
{
Console.WriteLine("this is the second derived class");
}
}
class Program
{
static void Main(string[] args)
{
var secondDrived = new SecondDerived();
secondDrived.Print(); //屏蔽了父类的方法
var myBaseClass = (MyBaseClass)secondDrived;
myBaseClass.Print(); //通过链条传递到最后拥有override的方法中
}
}
class MyBaseClass
{
virtual public void Print()
{
Console.WriteLine("This is the base class");
}
}
class Myderived:MyBaseClass
{
override public void Print()
{
Console.WriteLine("This is the derived class");
}
}
class SecondDerived:Myderived
{
new public void Print()
{
Console.WriteLine("this is the second derived class");
}
}
class Program
{
static void Main(string[] args)
{
var myDerived = new Myderived();
var myBase = (MyBaseClass)myDerived;
Console.WriteLine(myDerived.MyInt);
Console.WriteLine(myBase.MyInt);
}
}
class MyBaseClass
{
private int myInt = 5;
virtual public int MyInt
{
get { return myInt; }
}
}
class Myderived:MyBaseClass
{
private int myInt = 15;
override public int MyInt
{
get { return myInt; }
}
}
class SecondDerived:Myderived
{
private int myInt = 5;
override public int MyInt
{
get { return myInt; }
}
}
7.11抽象类:抽象类
class Program
{
static void Main(string[] args)
{
var Derived = new DerivedClass();
Derived.MyInt = 56;
Derived.PrintStuff("helloworld");
Derived.TriangLeSideCount = 4;
Derived.SideLength = 5;
Console.WriteLine(Derived.MyInt);
}
}
abstract class AbClass
{
public int SideLength = 10;//数据成员不可以声明为abstract
public int TriangLeSideCount = 3;
abstract public int MyInt { get; set; } //抽象属性
abstract public void PrintStuff(string s); //抽象方法
}
class DerivedClass:AbClass
{
public override void PrintStuff(string s)
{
Console.WriteLine(s);
}
private int _myInt;
public override int MyInt { get { return _myInt; } set { _myInt = value; } }
}