抽象类
class Test
{
public static void Main()
{
Console.Write(StudentInfo.Name);//输出ligang
Console.Read();
}
}
//静态方法一般使用静态成员
实例方法可以使用静态方法,而静态方法不能使用实例方法
static class StudentInfo
{
private static string name = "ganquanfu";
public static string Name
{
get { return StudentInfo.name; }
set { StudentInfo.name = value; }
}
static StudentInfo()//静态构造函数用于初始化静态成员,不能有任何的修饰符
{
name = "ligang";
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConAttribute
{
//注意抽象类可以没有抽象方法,但是反过来有抽象方法的类必须添加abstract,表示该类是抽象类
abstract class Animal
{
protected abstract void Show();//在实现该抽象方法的时候,修饰符也不要修改。
public void ValidateValue(object o)
{
Show();
}
}
class People : Animal
{
public People()
{
}
protected override void Show()
{
Console.WriteLine("People");
}
}
class Manager : People
{
/*//如何改为以下的代码后,结果将输出 People
protected new void Show()
{
Console.WriteLine("manager");
}*/
protected override void Show()
{
Console.WriteLine("manager");
}
}
class Test
{
public static void Main(string[] args)
{
Manager manager = new Manager();
manager.ValidateValue(new object());//输出manager
Console.ReadKey();
}
}
}
public static void Main(string[]args)
{
Person student = new Person();
student.Name = "甘全福";
student.Age = 23;
Person teacher = student.Clone();
Console.WriteLine("姓名:"+teacher.Name + "年龄:" + teacher.Age);
Console.WriteLine("克隆成功!");
Console.Read();
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person()
{
}
public Person(string name,int age)
{
this.Name = name;
this.Age = age;
}
public Person Clone()
{
Person p = new Person();
p.Name = this.Name;
p.Age = this.Age ;
return p;
}
}