• 2.1


    Animal.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Test
    {
        class Animal                     //创建基类
        {
            private bool m_sex;
            private int m_age;
            public bool Sex { get { return m_sex; } set { m_sex = value; } }
            public int Age { get { return m_age; } set { m_age = value; } }
            public Animal()
            {
                Sex = false;
            }
            public string introduce()
            {
                if (Sex)
                    return "This is a male Animal!";
                else
                    return "This is a female Animal!";     
            }
        }
        class Dog:Animal               //创建派生类并继承Animal
        {
            public Dog()
            {
                Sex = true;
            }
            public new string introduce()        //重载introduce函数
            {
                if (Sex)
                    return "This is a male Dog!";
                else
                    return "This is a female Dog!";
            }
        }
        class Cat : Animal                           //创建派生类并继承Animal
        {
            public Cat()
            {
                Sex = true;
            }
            public new string introduce()           //重载introduce函数
            {
                if (Sex)
                    return "This is a male Cat!";
                else
                    return "This is a female Cat!";
            }
        }
    
    }

    Program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                Animal ani = new Animal();
                Console.WriteLine(ani.introduce());
                Dog dog = new Dog();
                Console.WriteLine(ani.introduce());
                Cat cat = new Cat();
                Console.WriteLine(ani.introduce());
                Console.WriteLine("按回车键结束");
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    java 中类的加载顺序
    jdbc 连接数据库、批量操作、事务(oracle、mysql)
    一个空格引发的血案啊!
    Servlet 的生命周期与线程安全
    Java 方法的重写与重载
    java 序列化
    equals()与hashcode()的理解
    成功安装cadence SPB16.0
    stack implement C++
    windows 7 设置环境变量的方法
  • 原文地址:https://www.cnblogs.com/xx--/p/7594852.html
Copyright © 2020-2023  润新知