封装
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PropertyClass { class Program { static void Main(string[] args) { Myclass mycalss = new Myclass(); mycalss.Method(); } } class Myclass { public Myclass() { Console.WriteLine("Test class"); } public void Method(){ Method_1(); Method_2(); } private void Method_1() { Console.WriteLine("ONE"); } private void Method_2() { Console.WriteLine("TWO"); } } }
继承
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PropertyClass { class Program { static void Main(string[] args) { Myclass myclass = new Myclass(); myclass.Method(); Myclass1 myclass1 = new Myclass1(); myclass1.Method(); } } class Myclass { public Myclass() { Console.WriteLine("父类Test class"); } public void Method(){ Method_1(); Method_2(); } private void Method_1() { Console.WriteLine("父类ONE"); } private void Method_2() { Console.WriteLine("父类TWO"); } } class Myclass1:Myclass{ public Myclass1(){ Console.WriteLine("子类Test class"); } public void Method(){ Console.WriteLine("子类方法"); } } }
多态
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PropertyClass { class Program { static void Main(string[] args) { Myclass myclass = new Myclass(); myclass.Method(); Myclass1 myclass1 = new Myclass1(); myclass1.Method(); Myclass2 myclass2 = new Myclass2(); myclass2.Method(); } } class Myclass { public Myclass() { Console.WriteLine("父类测试"); } public virtual void Method (){ Console.WriteLine("父类方法"); } } class Myclass1 : Myclass { public Myclass1() { Console.WriteLine("子类1测试"); } public override void Method() { Console.WriteLine("子类1方法"); } } class Myclass2 : Myclass { public Myclass2() { Console.WriteLine("子类2测试"); } public override void Method() { Console.WriteLine("子类2方法"); } } }
时间: 2025-01-12 00:08:30