class Program
{
static void Main(string[] args)
{
//反射
Type t = typeof(Student);
object o = Activator.CreateInstance(t, 1, "FJ");
Student stu = o as Student;
Console.WriteLine(stu.Name);
//动态编程
dynamic stu2 = Activator.CreateInstance(t, 2, "XB");
Console.WriteLine(stu2.Name);
}
}
class Student
{
public Student(int id, string name)
{
this.ID = id;
this.Name = name;
}
public int ID { get; set; }
public string Name { get; set; }
}
静态构造器只能定义静态成员
声明一个类,创建一个实例或创建一个对象
class Program
{
static void Main(string[] args)
{
}
class Student
{
}
}
上面的Main方法是类成员,而Student是成员类
namespace MyLib.MyNamespace
{
internal class Calculator
{
public double Add(double a, double b)
{
return a + b;
}
}
}
namespace MyLib.MyNamespace2
{
class Student
{
public Calculator Calculator { get; set; }
}
}
其中MyNamespace和MyNamespace2在同一个assembly -- MyLib中。
namespace HelloClass
{
class Program
{
static void Main(string[] args)
{
Type t = typeof(Car);
Type tb = t.BaseType;
Type tTop = tb.BaseType;
Console.WriteLine(tTop.FullName);
//true
Console.WriteLine(tTop.BaseType==null);
Vehicle vehicle = new Vehicle();
//false
Console.WriteLine(vehicle is Car);
Car car = new Car();
//true
Console.WriteLine(car is Vehicle);
Vehicle vehicle2 = new Car();
Object o1 = new Vehicle();
Object o2 = new Car();
}
}
}
class Vehicle
{
}
class Car : Vehicle
{
}
继承的本质是派生类在基类已有的成员的基础上对基类进行横向和纵向的发展
//解决方案1
public Car() : base("N/A")
{
this.Owner = "Car Owner";
}
//解决方案2
public Car(string owner):base(owner)
{
}
class Vehicle
{
public virtual void Run()
{
Console.WriteLine("I'm running!");
}
}
class Car:Vehicle
{
public override void Run()
{
Console.WriteLine("Car is running!");
}
}
class Program
{
static void Main(string[] args)
{
Car car = new Car();
car.Run();
Console.WriteLine(car.Speed);
Vehicle vehicle = new Vehicle();
vehicle.Run();
Console.WriteLine(vehicle.Speed);
}
}
class Vehicle
{
private int _speed;
public virtual int Speed {
get {return _speed; }
set {_speed=value; }
}
public virtual void Run()
{
Console.WriteLine("I'm running!");
_speed = 100;
}
}
class Car:Vehicle
{
private int _rpm;
public override int Speed
{
get { return _rpm / 100; }
set { _rpm = value * 100; }
}
public override void Run()
{
Console.WriteLine("Car is running!");
_rpm = 5000;
}
}
多态的表现内容是当用一个父类类型的变量去引用子类类型实例的时候,当我们调用一个方法的时候,这个方法最终所被调用的版本是由对象的类型决定。它一定是能够调到这个继承链上最新的版本。最新的版本可能存在于我们的一个子类对象当中,也可能在重写过程当中被某一个隐藏给打断了
class Program
{
static void Main(string[] args)
{
Vehicle car = new Car();
car.Run();
Console.WriteLine(car.Speed);
Car vehicle = new RaceCar();
vehicle.Run();
}
}
class Vehicle
{
public virtual void Run()
{
Console.WriteLine("I'm running!");
}
}
class Car:Vehicle
{
public override void Run()
{
Console.WriteLine("Car is running!");
}
}
class RaceCar:Car
{
public override void Run()
{
Console.WriteLine("Race car is running !");
}
}
是五个面向对象基本设计原则的首字母的缩写,分别是:
single responsibility principle 单一职责原则(SRP)
open close principle 开闭原则(OCP)
liskov substitution principle 里氏替换原则(LSP)
interface segregation principle 接口隔离原则(ISP)
dependency inversion principle 依赖反转原则(DIP)
class Program
{
static void Main(string[] args)
{
Vehicle v = new RaceCar();
v.Run();
}
}
abstract class Vehicle
{
public void Stop()
{
Console.WriteLine("Stopped!");
}
public void Fill()
{
Console.WriteLine("Pay and fill...");
}
public abstract void Run();
}
class Car : Vehicle
{
public override void Run()
{
Console.WriteLine("Car is running!");
}
}
class Truck : Vehicle
{
public override void Run()
{
Console.WriteLine("Truck is running!");
}
}
class RaceCar : Vehicle
{
public override void Run()
{
Console.WriteLine("Race car is running!");
}
}
class Program
{
static void Main(string[] args)
{
Vehicle v = new RaceCar();
v.Run();
}
}
interface IVehicle
{
void Stop();
void Fill();
void Run();
}
abstract class Vehicle:IVehicle
{
public void Stop()
{
Console.WriteLine("Stopped!");
}
public void Fill()
{
Console.WriteLine("Pay and fill...");
}
public abstract void Run();
}
class Car : Vehicle
{
public override void Run()
{
Console.WriteLine("Car is running!");
}
}
class Truck : Vehicle
{
public override void Run()
{
Console.WriteLine("Truck is running!");
}
}
class RaceCar : Vehicle
{
public override void Run()
{
Console.WriteLine("Race car is running!");
}
}