继承+多态练习
2015-10-15 21:13
429 查看
//类的继承
//父类中如果没有声明一个构造函数,系统会自动分给他一个无参的构造函数
//子类在继承父类,会先调用父类中那个无参的构造函数
//如果父类中声明了有参的构造函数,那么那个默认的构造函数会被覆盖掉,这时子类就会报错
//可以把无参的构造函数手动写上,还可使用base()。下面程序全部由
//这跟类的构造函数一样,系统会默认给一个
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Student A = new Student(“ugik”,12,546);
Console.WriteLine(A.Name);
Console.WriteLine(A.Age);
Console.WriteLine(A.No);
Console.ReadKey();
}}class Person
{
public Person(String name)
{
this.Name = name;
}public Person(String name,int age)
{
this.Name = name;
this.Age = age;
}public String Name
{
get;
set;
}public int Age
{
get;
set;
}}
}2,/*定义父亲类Father(姓lastName,财产property, 血型bloodType)
,儿子Son类(玩游戏PlayGame方法),女儿Daughter类(跳舞Dance方法),
调用父类构造函数(:base())给子类字段赋值*/
class Faher
{
public Faher(String lastname,double property,BloodType bloodtype)
{
this.LastName = lastname;
this.Property = property;
this.bloodtype1 = bloodtype; //我靠,这里居然用的是bloodtype1属性
}public String LastName
{
get;
set;
}public double Property
{
get;
set;
}public BloodType bloodtype1 //我靠BloodType充当数据类型
{
get;
set;
}
public enum BloodType { A,B,AB,O} //定义血型枚举
}4,,多态
class Program
{
static void Main(string[] args)
{
Chinese cn1 = new Chinese(“小林”);
Chinese cn2 = new Chinese(“杜垏明”);
Japnese jp1 = new Japnese(“松下幸之助”);
Japnese jp2 = new Japnese(“盛田昭夫”);
American am = new American(“巴顿”);
5,,namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Chinese cn1 = new Chinese(“小林”);
Chinese cn2 = new Chinese(“杜垏明”);
Japnese jp1 = new Japnese(“松下幸之助”);
Japnese jp2 = new Japnese(“盛田昭夫”);
American am = new American(“巴顿”);
6//静态类,静态函数
//静态类不能被继承,也不能继承
//封装、继承的目的是为了重用代码、为了多态,而静态类不能继承,所以封装与静态时对立的
//静态类不能被继承,但是类中的静态方法可以.静态方法就是可以用类名直接点出来,不用创建对象而已。
class Program
{
static void Main(string[] args)
{
A.B(); //静态函数直接用类名点出来,不需要创建对象
AS();
Console.ReadKey();
}public static void AS()
{
A.B();
}}public class A
{
public A()
{
Console.WriteLine(“构造函数被调用了。。。”);
}public static void B()
{
Console.WriteLine(“我是谁”);
}
}00000000
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
Wo f = new Wo();
A.B();
Wo.B(); //这个难道是静态方法被继承了?
f.C();
Console.ReadKey();
}public static void AS()
{
A.B();
}}public class A
{
public A()
{
Console.WriteLine(“构造函数被调用了。。。”);
}public static void B()
{
Console.WriteLine(“我是谁”);
}public void C()
{
Console.WriteLine(“调用第三个函数”);
}}class Wo : A
{
}
//父类中如果没有声明一个构造函数,系统会自动分给他一个无参的构造函数
//子类在继承父类,会先调用父类中那个无参的构造函数
//如果父类中声明了有参的构造函数,那么那个默认的构造函数会被覆盖掉,这时子类就会报错
//可以把无参的构造函数手动写上,还可使用base()。下面程序全部由
//这跟类的构造函数一样,系统会默认给一个
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Student A = new Student(“ugik”,12,546);
Console.WriteLine(A.Name);
Console.WriteLine(A.Age);
Console.WriteLine(A.No);
Console.ReadKey();
}}class Person
{
public Person(String name)
{
this.Name = name;
}public Person(String name,int age)
{
this.Name = name;
this.Age = age;
}public String Name
{
get;
set;
}public int Age
{
get;
set;
}}
class Student : Person { public Student(String name, int age, int no):base(name) //调用的是父类中那个含一个参数的构造函数 { this.Name=name; this.Age = age; this.No = no; } public int No { get; set; } } class Teacher : Person { public Teacher(String name, int age, int salar) : base(name, age) //调用的是父类中那个含两个参数的构造函数 { this.Name = name; this.Age = age; this.Salar = salar; } public int Salar { get; set; } }
}2,/*定义父亲类Father(姓lastName,财产property, 血型bloodType)
,儿子Son类(玩游戏PlayGame方法),女儿Daughter类(跳舞Dance方法),
调用父类构造函数(:base())给子类字段赋值*/
class Faher
{
public Faher(String lastname,double property,BloodType bloodtype)
{
this.LastName = lastname;
this.Property = property;
this.bloodtype1 = bloodtype; //我靠,这里居然用的是bloodtype1属性
}public String LastName
{
get;
set;
}public double Property
{
get;
set;
}public BloodType bloodtype1 //我靠BloodType充当数据类型
{
get;
set;
}
} class Son:Faher { public Son(String lastname, double property, BloodType bloodtype) : base(lastname, property, bloodtype) { } public void PlayGame() { } } class Daughter:Faher { public Daughter(String lastname, double property, BloodType bloodtype) : base(lastname, property, bloodtype) { } public void Dance() { } }
public enum BloodType { A,B,AB,O} //定义血型枚举
}4,,多态
class Program
{
static void Main(string[] args)
{
Chinese cn1 = new Chinese(“小林”);
Chinese cn2 = new Chinese(“杜垏明”);
Japnese jp1 = new Japnese(“松下幸之助”);
Japnese jp2 = new Japnese(“盛田昭夫”);
American am = new American(“巴顿”);
Person[] cns=new Person[]{ cn1, cn2, jp1, jp2, am }; for (int i = 0; i < cns.Length; i++) { if (cns[i] is Chinese) { Chinese ch = (Chinese)cns[i]; ch.Say(); } else if (cns[i] is Japnese) { Japnese ch = (Japnese)cns[i]; ch.Say(); } else if(cns[i] is American) { American ch=(American)cns[i]; ch.Say(); } } Console.ReadKey(); } } class Person { public Person(String name) { this.Name = name; } public String Name { get; set; } } class Chinese:Person { public Chinese(String name):base(name) { } public void Say() { Console.WriteLine("我叫{0},我是中国人,,",Name); } } class Japnese : Person { public Japnese(String name) : base(name) { } public void Say() { Console.WriteLine("我叫{0},我是日本人,,", Name); } } class American : Person { public American(String name) : base(name) { } public void Say() { Console.WriteLine("我叫{0},我是美国人,,", Name); } }
5,,namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Chinese cn1 = new Chinese(“小林”);
Chinese cn2 = new Chinese(“杜垏明”);
Japnese jp1 = new Japnese(“松下幸之助”);
Japnese jp2 = new Japnese(“盛田昭夫”);
American am = new American(“巴顿”);
Person[] cns=new Person[]{ cn1, cn2, jp1, jp2, am }; for (int i = 0; i < cns.Length; i++) { cns[i].Say(); } Console.ReadKey(); } } } class Person { public Person(String name) { this.Name = name; } public String Name { get; set; } public virtual void Say() { Console.WriteLine("父类中的方法"); } } class Chinese:Person { public Chinese(String name):base(name) { } public override void Say() { Console.WriteLine("我叫{0},我是中国人,,",Name); } } class Japnese : Person { public Japnese(String name) : base(name) { } public override void Say() { Console.WriteLine("我叫{0},我是日本人,,", Name); } } class American : Person { public American(String name) : base(name) { } public override void Say() { Console.WriteLine("我叫{0},我是美国人,,", Name); } }
6//静态类,静态函数
//静态类不能被继承,也不能继承
//封装、继承的目的是为了重用代码、为了多态,而静态类不能继承,所以封装与静态时对立的
//静态类不能被继承,但是类中的静态方法可以.静态方法就是可以用类名直接点出来,不用创建对象而已。
class Program
{
static void Main(string[] args)
{
A.B(); //静态函数直接用类名点出来,不需要创建对象
AS();
Console.ReadKey();
}public static void AS()
{
A.B();
}}public class A
{
public A()
{
Console.WriteLine(“构造函数被调用了。。。”);
}public static void B()
{
Console.WriteLine(“我是谁”);
}
}
}00000000
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
Wo f = new Wo();
A.B();
Wo.B(); //这个难道是静态方法被继承了?
f.C();
Console.ReadKey();
}public static void AS()
{
A.B();
}}public class A
{
public A()
{
Console.WriteLine(“构造函数被调用了。。。”);
}public static void B()
{
Console.WriteLine(“我是谁”);
}public void C()
{
Console.WriteLine(“调用第三个函数”);
}}class Wo : A
{
}
}
相关文章推荐
- Android Studio Gradle编译问题
- js事件跨浏览器处理
- java核心技术之IO流(一)File类
- uboot 源码官方下载地址 2
- 进线程、库、第三范式
- spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creatio...
- navicat for mysql只导出数据表结构
- spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常
- BZOJ2705 [SDOI2012]Longge的问题
- hdu4778 状态压缩
- EntityFramework在root目录web.config中的配置设置
- [Java Web]Web应用基本概念
- swift 2.0 字典
- 51nod 1106 质数检测 (三种模板_好题)
- linux shell用法(1)-- tee命令
- 海岸线研究流水账
- 利用颜色矩阵实现图片效果
- 车站
- Javascript基础(三)
- NSURLErrorDomain Code=-1002 “unsupported URL”