继承+多态练习

//类的继承
//父类中如果没有声明一个构造函数,系统会自动分给他一个无参的构造函数
//子类在继承父类,会先调用父类中那个无参的构造函数
//如果父类中声明了有参的构造函数,那么那个默认的构造函数会被覆盖掉,这时子类就会报错
//可以把无参的构造函数手动写上,还可使用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
{

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值