首页 > 其他分享 >继承2

继承2

时间:2024-04-09 21:36:28浏览次数:13  
标签:name 继承 age System sex using public

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace x180821_汪敏_19_5_7_继承作业
{
    public class chinese : person//定义一个chinese派生类,继承自person类
    {
        public string shengxiao;//生肖
        public override void sayhello(string shengxiao, string name, int age, string sex)
        {
            base.sayhello();//调用基类的无参方法
            this.name = name;
            this.age = age;
            this.sex = sex;
            this.shengxiao = shengxiao;

            Console.WriteLine("你好!我的生肖是{0},我的名字叫{1},我今年{2}岁,我的性别是{3}。", shengxiao, name, age, sex);
        }
        public void hello()//无参构造函数
        {
            shengxiao = "牛";
            name = "小红";
            age = 10;
            sex = "女";
            Console.WriteLine("你好!我的生肖是{0},我的名字叫{1},我今年{2}岁,我的性别是{3}。", shengxiao, name, age, sex);
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace x180821_汪敏_19_5_7_继承作业
{
    
        public class english : person//定义一个english派生类,继承自person类
        {
            public string xingzuo;//星座
            public override void sayhello(string xingzuo, string name, int age, string sex)
            {
                base.sayhello();//调用基类的无参方法
                this.xingzuo = xingzuo;
                this.name = name;
                this.age = age;
                this.sex = sex;
                Console.WriteLine("hello!my star sign is {0},my name is {1},i'm {2} years old ,my sex is {3}.", xingzuo, name, age, sex);
            }
            public void hello()//无参构造函数
            {
                xingzuo = "巨蟹座";
                name = "小红";
                age = 10;
                sex = "女";
                Console.WriteLine("hello!my star sign is {0},my name is {1},i'm {2} years old ,my sex is {3}.", xingzuo, name, age, sex);
            }
        }
    
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace x180821_汪敏_19_5_7_继承作业
{
    //基类
    public class person
    {
        public string name;//姓名
        public int age;//年龄
        public string sex;//性别
        //虚方法--子类可以重写
        public virtual void sayhello(string x, string name, int age, string sex)//有参构造方法
        {
            this.name = name;
            this.age = age;
            this.sex = sex;
            Console.WriteLine("hello!我的名字叫{0},我今年{1}岁,我的性别是{2}", name, age, sex);
        }
        //无参构造方法
        public void sayhello()
        {
            name = "小红";
            age = 10;
            sex = "女";
            Console.WriteLine("hello!我的名字叫{0},我今年{1}岁,我的性别是{2}", name, age, sex);
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace x180821_汪敏_19_5_7_继承作业
{
    class Program
    {
       
       
       
        static void Main(string[] args)
        {
            //在主方法实例化
            
            person a = new person();
            english b = new english();
            chinese c = new chinese();

            Console.WriteLine("\t三个类的无参构造:");
            a.sayhello();
            b.hello();
            c.hello();

            Console.WriteLine("\t三个类的有参构造和子类调用父类的无参构造:");
            a.sayhello("","小明",12,"男");
            b.sayhello("cancer","xiaomin", 12, "male");
            c.sayhello("牛","小明", 12, "男");
            //停止看结果
            Console.ReadLine();
        }
    }
}

 

标签:name,继承,age,System,sex,using,public
From: https://www.cnblogs.com/bky-wang/p/18124862

相关文章

  • CSS -层叠性、继承性、盒子模型、盒子模型表格、盒子模型margin、盒子阴影
    层叠性CSS层叠性(Cascading)是指在网页中应用多个样式规则时,根据一定的规则来确定最终应用的样式。层叠性使得样式可以按照一定的优先级和规则进行组合和覆盖,从而实现对元素的样式控制。层叠性的影响因素:选择器的特殊性(Specificity):选择器的特殊性决定了样式规则的优先级。......
  • Java——继承(含习题)
    继承的概念定义面向对象的继承,指在由一般类和特殊类形成的“一般-特殊”之间的类结构中,把一般类和所有特殊类都共同具有的属性和操作一次性地在一般类中进行定义,特殊类不再重复定义一般类已经定义的属性和操作,特殊类自动拥有一般类(以及所有更上层的一般类)定义的属性和操作......
  • C++继承之protected继承
    1概述  通过继承机制,可以利用已有的对象类型来定义新对象类型。所定义的新对象类型不仅仅拥有新定义的成员,而且还同时拥有旧的成员。我们称已存在的用来派生新类的类为基类,又称为父类。由已存在的类派生出的新类称为派生类,又称为子类。。2对象访问作用域作用域:publi......
  • C++ Note 继承指向
    引自:https://blog.csdn.net/baidu_35679960/article/details/80812527引自:https://blog.csdn.net/qq_21989927/article/details/111226696引自:https://www.runoob.com/cplusplus/cpp-polymorphism.html 为什么父类指针可以指向子类实例:可以通俗的理解,子类可能含有一些父类没......
  • C++:类的继承
    基类的构造函数和析构函数不会被继承,但是当子类对象初始化时则会自动调用基类的构造函数和析构函数(默认)如果想要调用基类的重载构造函数,需要在构造函数后加上“:<重载的构造函数>{};”,如下classFATHER{public:FATHER();~FATHER();FATHER(inta)//重载......
  • C++核心编程:多继承
    #include<iostream>usingnamespacestd;//多继承语法继承方式父类1,继承方式父类2//实际开发过程中,一般不建议使用多继承classBase1{public: Base1() { m_A=100; } intm_A;};classBase2{public: Base2() { m_A=200; } intm_A;};cl......
  • Java中的继承
    一、继承1.1为什么需要继承在写代码过程中两个类之间可能存在大量重复的代码,如何把这些重复的代码提取出来简化代码呢?面向对象思想中提出了继承的概念,专门用来进行共性抽取,实现代码复用。1.2继承的概念 继承(inheritance):是面向对象程序设计使代码可以复用的最重要......
  • 1600. 王位继承顺序
    题面核心思想纯模拟!没反应过来是前序遍历~privateMap<String,List<String>>children;表示一个人的孩子当调用getInheritanceOrder时通过map搜索答案,由于孩子也有可能有孩子,无限套娃,所以通过递归搜索建立答案。建立答案的时候也不用考虑是否死亡,我们搜索完成后在去删除......
  • python面向对象(二)继承:最直接的代码复用
    继承简介继承是指通过在创建类时提供另一个类(称为父类)的名称,来获取父类的属性与方法。继承最明显的作用就是节约代码量,不需要重复定义已经存在的属性与方法。但是深入思考就会发现,复用只是继承的附带作用,继承最有意义的一点,是将不同的类联系了起来,让不同的类之间能够有一定的共性......
  • CSS样式继承
    CSS样式继承是指子元素会继承父元素的某些样式属性。常见的可以继承的CSS属性包括font-family、color、font-size、line-height。1.color子元素会继承父元素的文字颜色。.parent{color:blue;}.child{/*子元素继承父元素的文字颜色*/}2.font-size......