zoukankan      html  css  js  c++  java
  • C# 中类与继承等概念

    构造函数/析构函数:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
    
        class Person
        {
            public int Number;
            public string Name;
            private int tmp = 0;
    
            // 定义构造函数
            public Person(int x,string y)
            {
                this.Number = x;
                this.Name = y;
                Console.WriteLine("构造函数执行");
            }
            // 定义析构函数
            ~Person()
            {
                Console.WriteLine("析构函数执行");
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Person ptr = new Person(1001, "lyshark");
                Console.WriteLine("ID: {0} Name: {1}", ptr.Number, ptr.Name);
    
                Console.ReadKey();
            }
        }
    }
    

    对象中Get/Set方法: 该方法是用来限定用户属性的。

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
    
        class Person
        {
            private int _age;
            public int age
            {
                // 当用户输出该属性的值是执行Get方法
                get { return _age; }
    
                // 当用户对该属性赋值时执行Set方法
                set
                {
                    // 判断如果年龄小于0或者大于100则直接返回0
                    if (value < 0 || value > 100)
                    {
                        value = 0;
                    }
                    // 否则将用户数据赋值给变量
                    _age = value;
                }
            }
            // 定义构造函数
            public Person(int x)
            {
                this.age = x;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Person ptr = new Person(10);
                Console.WriteLine("正常年龄: {0}", ptr.age);
    
                Person ptr_err = new Person(102);
                Console.WriteLine("异常年龄: {0}", ptr_err.age);
    
                Console.ReadKey();
            }
        }
    }
    

    静态方法与非静态方法:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Person
        {
            private static string _name;
            public static string Name
            {
                get { return Person._name; }
                set { Person._name = value; }
            }
    
            private char _gender;
            public char Gender
            {
                get { return _gender; }
                set { _gender = value; }
            }
            public void M1()
            {
                Console.WriteLine("我是非静态的方法");
            }
            public static void M2()
            {
                Console.WriteLine("我是一个静态方法");
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Person ptr = new Person();
    
                // 调用非静态方法
                ptr.M1();
                // 调用静态方法
                Person.M2();
    
                Console.ReadKey();
            }
        }
    }
    

    继承的基本使用:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        // 定义基类
        public class Person
        {
            public string name;
            public int age;
    
            public Person(string _name, int _age)
            {
                this.name = _name;
                this.age = _age;
            }
            public void Display()
            {
                Console.WriteLine("姓名: {0} 年龄:{1} ", this.name, this.age);
            }
        }
    
        // 定义派生类
        public class Studnet: Person
        {
            public int stu;
            public Studnet(string _name,int _age,int _stu):base(_name,_age)
            {
                this.stu = _stu;
            }
            public void Display()
            {
                Console.WriteLine("Stu编号: {0} 学生姓名: {1} 年龄: {2}",this.stu,this.name,this.age);
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                // 定义基类
                Person ptr = new Person("lyshark",22);
                ptr.Display();
    
                // 定义派生类
                Studnet stu = new Studnet("lyshark",22,1001);
                stu.Display();
    
                Console.ReadKey();
            }
        }
    }
    

    虚方法实现多态: 首先将父类函数标记为虚方法,然后子类就可以重写父类的虚方法,实现多态。

    using System;
    using System.Collections;
    
    namespace ConsoleApplication1
    {
        // 定义一个人类的基类
        public class Person
        {
            public string name;
            public Person(string name)
            {
                this.name = name;
            }
            public virtual void SayHello()
            {
                Console.WriteLine("基类方法");
            }
        }
        // 定义日本人并继承人类,重写SayHello方法
        public class Japanese : Person
        {
            public Japanese(string name) : base(name) { }
            // 重写父类的SayHello
            public override void SayHello()
            {
                base.SayHello();
                Console.WriteLine("日本人");
            }
        }
        // 定义中国并继承人类,重写SayHello方法
        public class Chinese : Person
        {
            public Chinese(string name) : base(name) { }
            // 重写父类的SayHello
            public override void SayHello()
            {
                Console.WriteLine("中国人");
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                // 普通的调用方式
                Japanese jap = new Japanese("苍井空");
                jap.SayHello();
    
                Chinese chi = new Chinese("韩梅梅");
                chi.SayHello();
    
                // 直接通过循环调用
                Person[] per = { jap, chi };
                for (int x = 0; x < per.Length; x++)
                    per[x].SayHello();
    
                Console.ReadKey();
            }
        }
    }
    

    抽象类实现多态: 当父类中的方法不知道如何实现的时候,可以考虑将父类定义为抽象类,将方法定义为抽象方法。

    using System;
    using System.Collections;
    
    namespace ConsoleApplication1
    {
        // 定义一个抽象类,等待让子类重写
        public abstract class Shape
        {
            public abstract double GetArea();
        }
    
        // 定义子类重写抽象类
        public class CirCle:Shape
        {
            public double x;
            public double y;
    
            public CirCle(double x,double y)
            {
                this.x = x;
                this.y = y;
            }
            public override double GetArea()
            {
                return this.x * this.y;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                // 抽象类不可实例化,只能实例化子类
                Shape sp = new CirCle(12.5,20.8);
                double ret = sp.GetArea();
                Console.WriteLine("结果是: {0}", ret);
    
                Console.ReadKey();
            }
        }
    }
    

    接口实现多态: 接口不允许有访问修饰符,方法自动设置为自动属性。

    using System;
    using System.Collections;
    
    namespace ConsoleApplication1
    {
        // 定义一个接口,等待让子类重写
        public interface Person
        {
            void Display();
        }
        // 定义派生类,继承于Person接口
        public class Student:Person
        {
            public void Display()
            {
                Console.WriteLine("定义学生");
            }
        }
        // 定义派生类
        public class Teacher:Person
        {
            public void Display()
            {
                Console.WriteLine("定义老师");
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                // 调用学生方法
                Person stu = new Student();
                stu.Display();
    
                // 调用老师方法
                Person tea = new Teacher();
                tea.Display();
    
                Console.ReadKey();
            }
        }
    }
    

    版权声明: 本博客,文章与代码均为学习时整理的笔记,博客中除去明确标注有参考文献的文章,其他文章【均为原创】作品,转载请务必【添加出处】,您添加出处是我创作的动力!

    警告:如果您恶意转载本人文章,则您的整站文章,将会变为我的原创作品,请相互尊重!
  • 相关阅读:
    匈牙利算法
    Tabbed Activity = viewpager + fragment ?
    gdb调试多线程多进程
    gdb 调试,当发现程序退出,需要定位程序退出位置时。
    将Linux的信号量sem_t封装成事件对象
    Golang包管理工具govendor的使用&go mod
    go get命令详解
    GoLand生成可执行文件(Windows、Linux)
    Linux下线程pid和tid
    理解Linux的进程,线程,PID,LWP,TID,TGID
  • 原文地址:https://www.cnblogs.com/LyShark/p/13156973.html
Copyright © 2011-2022 走看看