protected继承的可以访问,外部不可以访问
先调用基类的构造函数,之后在构造子类的构造函数
base可以决定调用基类的哪个构造函数
using System;
namespace test7
{
class SecretData
{
public int Age { get; set; }
}
class Animal
{
protected SecretData SecretData=new SecretData();
public string Name { get; set; }
}
class Dog : Animal//继承
{
public void Shout()
{
Console.WriteLine("{0}岁的"+base.Name + ":旺旺",SecretData.Age);
}
}
class A
{
public A() { Console.WriteLine("A()"); }
public A(int a) : this() { Console.WriteLine("A(int)"); }
}
class B : A
{
public B():base(123){ Console.WriteLine("B()"); }
}
class Program
{
static void Main(string[] args)
{
B b = new B();
}
}
}
使父类方法对应不同的子类展示相应的方法
using System;
namespace test7
{
class SecretData
{
public int Age { get; set; }
}
class Animal
{
protected SecretData SecretData=new SecretData();
public string Name { get; set; }
public virtual void Shout() { Console.WriteLine("nishidahsaiz"); }
}
class Dog : Animal//继承
{
public override void Shout()
{
Console.WriteLine("{0}岁的"+base.Name + ":旺旺",SecretData.Age);
}
}
class Program
{
static void Test(Animal animal)
{
Console.WriteLine(animal.Name);
animal.Shout();
}
static void Main(string[] args)
{
Animal animal = new Dog() { Name = "小河" };
//Dog animal = new Dog() { Name = "小河" };
Test(animal);
}
}
}
using System;
using System.Collections.Generic;
namespace test7_1
{
abstract class DashBoard //表盘
{
public abstract void Render();
}
class 雇佣兵的HP: DashBoard
{
public int Hp = 200;
public override void Render() { Console.WriteLine("你是雇佣兵{0}", this.Hp); }
}
class 主角的HP: DashBoard
{
public int Hp = 100;
public override void Render() { Console.WriteLine("你是主角{0}",this.Hp); }
}
class 主角
{
public 主角的HP HP = new 主角的HP();
public 雇佣兵 Helper = new 雇佣兵();
}
class 雇佣兵
{
public 雇佣兵的HP HP = new 雇佣兵的HP();
}
class Program
{
static void 重绘(List<DashBoard> dashboards)
{
foreach(var dashboard in dashboards)
dashboard.Render();
}
static void Main(string[] args)
{
主角 me = new 主角();
List<DashBoard> a = new List<DashBoard>();
a.Add(me.HP);
a.Add(me.Helper.HP );
重绘(a);
me.HP.Hp = 2222;
重绘(a);
Console.WriteLine("Hello World!");
}
}
}