构造函数 封装 继承 多态

构造函数

一个类里可以有多个构造函数,参数和数量不同

House wanda = new House(); 构造函数加括号的意义是实例化成功了,会自动调用构造函数

一般都是public   --->    public Person()   <---方法名必须与类名一样,且没有返回值
                        {

                        }

带参构造函数

public Person(string name, int age)
{
    this.name = name;
    this.age = age;
    Console.WriteLine("我出生啦!");
} 
public House(int personCount)
{
    owners = new Person[personCount];
} 

构造函数的重载

要是这样写可太麻烦了。。。。

//创建一个类
class Player{
    //随便声明一些字段
    public int id = 0;
    public string name = "";
    public int atk = 10;
    public int hp = 10;
    
    //构造函数
    public Player(){
        name = "xiaoming";
    }
    
    public Player(string name){
        this.name = name;
    }
    
    public Player(string name, int atk){
        this.name = name;
        this.atk = atk;
    }
    
     public Player(string name, int atk, int hp){
        this.name = name;
        this.atk = atk;
        this.hp = hp;
    }
}

class Program{
    static void Main(string[] args){
        Player player = new Player();
        Player player = new Player("小明");
        Player player = new Player("小明", 100);
        Player player = new Player("小明", 100, 1000);
 
    }
}

修改

//创建一个类
class Player{
    //随便声明一些字段
    public int id = 0;
    public string name = "";
    public int atk = 10;
    public int hp = 10;
    
    //构造函数
    //空构造函数系统默认的,当写了构造函数,那么就不能调用,如果没写构造函数,空构造函数可写可不写
    public Player(){
    }
    
    public Player(string name){
        this.name = name;
        Console.WriteLine("一个参数的构造");
    }
    
    public Player(string name, int atk) : this(name)
    {
        this.atk = atk;
        Console.WriteLine("二个参数的构造");
        
    }

    public Player(string name, int atk, int hp) : this(name, atk)
    {
        this.hp = hp;
        Console.WriteLine("三个参数的构造");
    }
}

class Program{
    static void Main(string[] args){
        Player player = new Player();
        Player player = new Player("小明");
        Player player = new Player("小明", 100);
        Player player = new Player("小明", 100, 1000);
 
    }
}

面向对象三大特征

封装,继承,多态

封装

封装的意思就是说一大堆代码格式化他这就叫封装,有可能不太好理解,举个例子 比如我们之前写的代码,途中有一个功能要重复使用,我们一般会选择放在一个方法里,然后直接调用方法,这就是封装的一种。第二种是比如一堆代码,然后放在类里。总之封装是一种很模糊的概念。

继承

就可以理解为儿子继承爸爸

//继承 
//这样写真的好啰嗦,虽然有ctrl+c大法,但是改的时候不好改啊
//重复的部分就需要继承
class Enemy
{
    public string name;
    public int atk = 3;
    public int hp = 10;
} 

class Boss
{
	public string name;
    public int atk = 3;
    public int hp = 10;
    
    
    public int satk = 3;
    public int miss = 10;
    public int[] skill = {……};
} 

class 类名 : 继承的类名 <----C#语言不支持多继承,所以只能继承一个父类,只有一个亲爹
	子类(派生类): 父类
要注意的是访问修饰符的使用,如果用了私有的修饰符,那么子类就不能继承这个

举个例子

class Enemy{
    public string name;
    public int atk;//如果把这个设置成protected那么底下的main函数都会报错
    public int hp;//比如把这个设置成private那么底下子类和main函数都会报错
}
class Boss : Enemy{
    public int satk;
    public int miss;
    //在子类的内部里也可以访问
    public Boss(){
        boss.name = "aaaa";
        boss.hp = 5;
        boss.atk = 10;
    }
}

class Program{
    static void Main(string[] args){
        Boss boss = new Boss();
        boss.name = "aaaa";
        boss.hp = 5;
        boss.atk = 10;
    }
}

构造函数的继承

public Person(string name)
{
    this.name = name;
} 

public Programmer(string name, int age,string language) : base(name)
{
	this.language = language;
} 

多态

比如现在有一个父类是角色,里面有一个方法是杀野怪

那么如果玩家选的是忍者这个角色的话,用的是忍术击杀的野怪

要是用武士呢,用的是宝剑击杀的敌人

所谓的多态就是父类做的事情,到子类里边展示出来的行为是不一样的

转型

比如 盗贼这个职业,要转成敌人这个大类,这就是向上转型,反过来敌人转盗贼就是向下转型

//盗贼转敌人
Enemy enemy = new DaoZei(“盗贼");     				
//这两个是敌人转盗贼
DaoZei daozei = (DaoZei)enemy; 
DaoZei daozei = enemy as DaoZei; 
//is用法 变量是不是这个类
if(sunyue is HeNanPerson) 


//这是正常的 先实例化盗贼 然后盗贼转敌人 敌人再转盗贼  因为本身就是子类对象转成父类,再从父类转成子类,是没有问题的 
//接下来是报错的
Enemy enemy = new Enemy();
Boss boss = (Boss)enemy;
//多态:父类变量指向子类实体
//不报错的
Enemy enemy = new Enemy();
if(enemy is Boss){
	Boss boss = (Boss)enemy;
}

覆盖 重写

new 关键词就覆盖了

class Person{
    public void SayHello()
    {
    	Console.WriteLine("干什么呢?");
    }
} 
class HeNanPerson : Person
{ 
 	public new void SayHello()
    {
        Console.WriteLine("弄啥嘞");
    } 
}

virtual 虚方法 override 重写

class Person{
    public virtual void SayHello()
    {
        Console.WriteLine("干什么呢?");
    }
} 
class HeNanPerson : Person{ 
    public override  void SayHello()
    {
        Console.WriteLine("弄啥嘞");
    } 
}
class TieLingPerson : Person{
    public override void SayHello()
    {
       base.Test();
       Console.WriteLine("嘎哈呢");
    } 
} 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值