C#学习第五天
今天进行了C#的第五次学习,继续了解C#的相关知识:
继承
继承就是基类派生出去多种类,就好比狗是哺乳动物,狗是派生类,哺乳动物是基类。
下面是一个简单的继承:
using System; namespace InheritanceApplication { class Shape { public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } protected int width; protected int height; } // 派生类 class Rectangle: Shape { public int getArea() { return (width * height); } } class RectangleTester { static void Main(string[] args) { Rectangle Rect = new Rectangle(); Rect.setWidth(5); Rect.setHeight(7); // 打印对象的面积 Console.WriteLine("总面积: {0}", Rect.getArea()); Console.ReadKey(); } } }
我们也可以进行多重继承:
using System; namespace InheritanceApplication { class Shape { public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } protected int width; protected int height; } // 基类 PaintCost public interface PaintCost { int getCost(int area); } // 派生类 class Rectangle : Shape, PaintCost { public int getArea() { return (width * height); } public int getCost(int area) { return area * 70; } } class RectangleTester { static void Main(string[] args) { Rectangle Rect = new Rectangle(); int area; Rect.setWidth(5); Rect.setHeight(7); area = Rect.getArea(); // 打印对象的面积 Console.WriteLine("总面积: {0}", Rect.getArea()); Console.WriteLine("油漆总成本: ${0}" , Rect.getCost(area)); Console.ReadKey(); } } }
多态性
静态多态性
函数重载
函数重载就是一个函数可以通过传入不同的参数来进行不同的运算,例如:
using System; namespace PolymorphismApplication { public class TestData { public int Add(int a, int b, int c) { return a + b + c; } public int Add(int a, int b) { return a + b; } } class Program { static void Main(string[] args) { TestData dataClass = new TestData(); int add1 = dataClass.Add(1, 2); int add2 = dataClass.Add(1, 2, 3); Console.WriteLine("add1 :" + add1); Console.WriteLine("add2 :" + add2); } } }
运算符重载
用法与函数重载相同:
using System; namespace PolymorphismApplication { class Printdata { void print(int i) { Console.WriteLine("输出整型: {0}", i ); } void print(double f) { Console.WriteLine("输出浮点型: {0}" , f); } void print(string s) { Console.WriteLine("输出字符串: {0}", s); } static void Main(string[] args) { Printdata p = new Printdata(); // 调用 print 来打印整数 p.print(1); // 调用 print 来打印浮点数 p.print(1.23); // 调用 print 来打印字符串 p.print("Hello Runoob"); Console.ReadKey(); } } }
动态多态性
可以通过abstract
关键字来创建一个抽象类,里面有抽象方法,可以被派生类实现。
我们也可以用sealed
关键字来实现密封,密封之后的类不能够被继承且无法使用抽象类。
using System; namespace PolymorphismApplication { abstract class Shape { abstract public int area(); } class Rectangle: Shape { private int length; private int width; public Rectangle( int a=0, int b=0) { length = a; width = b; } public override int area () { Console.WriteLine("Rectangle 类的面积:"); return (width * length); } } class RectangleTester { static void Main(string[] args) { Rectangle r = new Rectangle(10, 7); double a = r.area(); Console.WriteLine("面积: {0}",a); Console.ReadKey(); } } }
如果我们想在继承类里实现基类的方法,可以用虚方法,关键字virtual
来实现。
动态多态性是通过抽象类和虚方法实现的。
using System; using System.Collections.Generic; public class Shape { public int X { get; private set; } public int Y { get; private set; } public int Height { get; set; } public int Width { get; set; } // 虚方法 public virtual void Draw() { Console.WriteLine("执行基类的画图任务"); } } class Circle : Shape { public override void Draw() { Console.WriteLine("画一个圆形"); base.Draw(); } } class Rectangle : Shape { public override void Draw() { Console.WriteLine("画一个长方形"); base.Draw(); } } class Triangle : Shape { public override void Draw() { Console.WriteLine("画一个三角形"); base.Draw(); } } class Program { static void Main(string[] args) { // 创建一个 List<Shape> 对象,并向该对象添加 Circle、Triangle 和 Rectangle var shapes = new List<Shape> { new Rectangle(), new Triangle(), new Circle() }; // 使用 foreach 循环对该列表的派生类进行循环访问,并对其中的每个 Shape 对象调用 Draw 方法 foreach (var shape in shapes) { shape.Draw(); } Console.WriteLine("按下任意键退出。"); Console.ReadKey(); } }
运算符重载
运算符重载在我看来就是让运算更加的方便省事,可以节省代码。
举个例子:
using System; namespace OperatorOvlApplication { class Box { private double length; // 长度 private double breadth; // 宽度 private double height; // 高度 public double getVolume() { return length * breadth * height; } public void setLength( double len ) { length = len; } public void setBreadth( double bre ) { breadth = bre; } public void setHeight( double hei ) { height = hei; } // 重载 + 运算符来把两个 Box 对象相加 public static Box operator+ (Box b, Box c) { Box box = new Box(); box.length = b.length + c.length; box.breadth = b.breadth + c.breadth; box.height = b.height + c.height; return box; } public static bool operator == (Box lhs, Box rhs) { bool status = false; if (lhs.length == rhs.length && lhs.height == rhs.height && lhs.breadth == rhs.breadth) { status = true; } return status; } public static bool operator !=(Box lhs, Box rhs) { bool status = false; if (lhs.length != rhs.length || lhs.height != rhs.height || lhs.breadth != rhs.breadth) { status = true; } return status; } public static bool operator <(Box lhs, Box rhs) { bool status = false; if (lhs.length < rhs.length && lhs.height < rhs.height && lhs.breadth < rhs.breadth) { status = true; } return status; } public static bool operator >(Box lhs, Box rhs) { bool status = false; if (lhs.length > rhs.length && lhs.height > rhs.height && lhs.breadth > rhs.breadth) { status = true; } return status; } public static bool operator <=(Box lhs, Box rhs) { bool status = false; if (lhs.length <= rhs.length && lhs.height <= rhs.height && lhs.breadth <= rhs.breadth) { status = true; } return status; } public static bool operator >=(Box lhs, Box rhs) { bool status = false; if (lhs.length >= rhs.length && lhs.height >= rhs.height && lhs.breadth >= rhs.breadth) { status = true; } return status; } public override string ToString() { return String.Format("({0}, {1}, {2})", length, breadth, height); } } class Tester { static void Main(string[] args) { Box Box1 = new Box(); // 声明 Box1,类型为 Box Box Box2 = new Box(); // 声明 Box2,类型为 Box Box Box3 = new Box(); // 声明 Box3,类型为 Box Box Box4 = new Box(); double volume = 0.0; // 体积 // Box1 详述 Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // Box2 详述 Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0); // 使用重载的 ToString() 显示两个盒子 Console.WriteLine("Box1: {0}", Box1.ToString()); Console.WriteLine("Box2: {0}", Box2.ToString()); // Box1 的体积 volume = Box1.getVolume(); Console.WriteLine("Box1 的体积: {0}", volume); // Box2 的体积 volume = Box2.getVolume(); Console.WriteLine("Box2 的体积: {0}", volume); // 把两个对象相加 Box3 = Box1 + Box2; Console.WriteLine("Box3: {0}", Box3.ToString()); // Box3 的体积 volume = Box3.getVolume(); Console.WriteLine("Box3 的体积: {0}", volume); //comparing the boxes if (Box1 > Box2) Console.WriteLine("Box1 大于 Box2"); else Console.WriteLine("Box1 不大于 Box2"); if (Box1 < Box2) Console.WriteLine("Box1 小于 Box2"); else Console.WriteLine("Box1 不小于 Box2"); if (Box1 >= Box2) Console.WriteLine("Box1 大于等于 Box2"); else Console.WriteLine("Box1 不大于等于 Box2"); if (Box1 <= Box2) Console.WriteLine("Box1 小于等于 Box2"); else Console.WriteLine("Box1 不小于等于 Box2"); if (Box1 != Box2) Console.WriteLine("Box1 不等于 Box2"); else Console.WriteLine("Box1 等于 Box2"); Box4 = Box3; if (Box3 == Box4) Console.WriteLine("Box3 等于 Box4"); else Console.WriteLine("Box3 不等于 Box4"); Console.ReadKey(); } } }
接口
接口也是用关键字interface
来进行定义,与Java一样。
using System; interface IParentInterface { void ParentInterfaceMethod(); } interface IMyInterface : IParentInterface { void MethodToImplement(); } class InterfaceImplementer : IMyInterface { static void Main() { InterfaceImplementer iImp = new InterfaceImplementer(); iImp.MethodToImplement(); iImp.ParentInterfaceMethod(); } public void MethodToImplement() { Console.WriteLine("MethodToImplement() called."); } public void ParentInterfaceMethod() { Console.WriteLine("ParentInterfaceMethod() called."); } }
本文作者:信2005-2刘海涛
本文链接:https://www.cnblogs.com/lht020321/p/16792011.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步