using System;
namespace ConsoleApp1
{
class Box
{
public double length;//长度
public double breadth;//宽度
public double height;//高度
}
class BoxV2
{
private double length;
private double breadth;
private double height;
public void setLength(double l)
{
length = l;
}
public void setBreadth(double b)
{
breadth = b;
}
public void setHeight(double h)
{
height = h;
}
public double getVolume()
{
return length * breadth * height;
}
}
class Line
{
private double length;//线条的长度
public Line()
{
Console.WriteLine("对象已创建!");
}
public void setLength(double l)
{
length =l;
}
public double getLength()
{
return length;
}
}
class LineV2
{
private double length;//线条的长度
public LineV2(double l)
{
length = l;
}
public void setLength(double l)
{
length = l;
}
public double getLength()
{
return length;
}
}
class LineV3
{
private double length;//线条的长度
public LineV3()
{
Console.WriteLine("对象已创建!");
}
~LineV3()
{
Console.WriteLine("对象已删除!");
}
public void setLength(double l)
{
length = l;
}
public double getLength()
{
return length;
}
}
class StaticVar
{
public static int num;
public void count()
{
num++;
}
public int getNum()
{
return num;
}
}
class StaticFun
{
public static int num;
public void count()
{
num++;
}
public static int getNum()
{
return num;
}
}
class Shape
{
protected int width;
protected int height;
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
}
class Rectangle: Shape
{
public int getArea()
{
return (width * height);
}
}
class RectangleA
{
protected double length;
protected double width;
public RectangleA(double l, double w)
{
length = l;
width = w;
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("长度: {0}", length);
Console.WriteLine("宽度: {0}", width);
Console.WriteLine("面积: {0}", GetArea());
}
}
class Tabletop: RectangleA
{
private double cost;
public Tabletop(double l, double w) : base(l, w)
{ }
public double GetCost()
{
double cost;
cost = GetArea() * 70;
return cost;
}
public void Display()
{
base.Display();
Console.WriteLine("成本: {0}", GetCost());
}
}
public interface PaintCost
{
int getCost(int area);
}
class RectangleB: Shape, PaintCost
{
public int getArea()
{
return width * height;
}
public int getCost(int area)
{
return area * 57;
}
}
class Program
{
static void Main(string[] args)
{
Box box1 = new Box();
double volume = 0.0;
box1.height = 5.0;
box1.length = 6.0;
box1.breadth = 7.0;
volume = box1.height * box1.length * box1.breadth;
Console.WriteLine("box1的体积: {0}", volume);
BoxV2 box2 = new BoxV2();
box2.setBreadth(7.0);
box2.setHeight(5.0);
box2.setLength(6.0);
Console.WriteLine("box2的体积: {0}", box2.getVolume());
Line line1 = new Line();
line1.setLength(6.0);
Console.WriteLine("line1线条的长度:{0}", line1.getLength());
LineV2 line2 = new LineV2(6.0);
Console.WriteLine("line2线条的长度:{0}", line2.getLength());
//析构函数
{
LineV3 line3 = new LineV3();
line3.setLength(5.70);
Console.WriteLine("line3线条的长度:{0}", line3.getLength());
}
//静态成员变量
StaticVar s1 = new StaticVar();
StaticVar s2 = new StaticVar();
s1.count();
s1.count();
s1.count();
s2.count();
s2.count();
Console.WriteLine("s1的变量num: {0}", s1.getNum());
Console.WriteLine("s2的变量num: {0}", s2.getNum());
//静态成员函数
StaticFun s3 = new StaticFun();
s3.count();
s3.count();
s3.count();
Console.WriteLine("变量 num: {0}", StaticFun.getNum());
//继承
//一个类可以派生自多个类或接口,这意味着它可以从多个积累或接口继承数据和函数。
/*
* <访问修饰符> class <基类>
* {
* ...
* }
* class <派生类>:<基类>
* {
* ...
* }
*/
Rectangle rect = new Rectangle();
rect.setHeight(6);
rect.setWidth(5);
Console.WriteLine("总面积:{0}", rect.getArea());
//基类的初始化
//派生类继承了基类的成员变量和成员方法,因此父类对象应在子类对象创建之前被创建。你可以在成员初始化列表中进行父类初始化。
Tabletop tt = new Tabletop(4.5, 7.5);
tt.Display();
//多重继承
//多重继承指的是一个类别可以同时从多于一个父类继承行为与特征的功能。与单一继承相对,单一继承指一个类别只可以继承自一个父类。
//C#不支持多重继承。但是,可以使用接口来实现多重继承。
RectangleB rb = new RectangleB();
int area;
rb.setWidth(5);
rb.setHeight(7);
area = rb.getArea();
Console.WriteLine("总面积:{0}", rb.getArea());
Console.WriteLine("油漆成本:{0}", rb.getCost(area));
//多态性
//静态多态性:函数重载、运算符重载
//函数重载:你可以在同一个范围内对相同的函数名有多个定义。函数的定义必须彼此不同,可以是参数列表中的参数类型不同,也可以是参数个数不同。不能重载只有返回类型不同的函数声明。
/*
* 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;
* }
* }
* public class PrintData
* {
* void print(int i)
* {
* Console.WriteLine("输出整型:{0}", i);
* }
* void print(double d)
* {
* Console.WriteLine("输出浮点型:{0}", d);
* }
* void print(string s)
* {
* Console.WriteLine("输出字符串:{0}", s);
* }
* }
*/
//动态多样性
//C#允许使用abstract创建抽象类,用于提供接口的部分类的实现。当一个派生类继承自该抽象类时,实现即完成。抽象类包含抽象方法,抽象方法可以被派生类实现,派生类具有更专业的功能。
/*
* 抽象类的一些规则
* 不能创建一个抽象类的实例。
* 不能在一个抽象类外部声明一个抽象方法。
* 通过在类定义前面放置关键字sealed,可以将类声明为密封类。当一个类被声明为sealed时,它不能被继承。抽象类不能被声明为sealed。
* 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);
* }
* }
*/
//当有一个定义在类中的函数需要在继承类中实现时,可以使用虚方法。
//虚方法是使用关键字virtual声明的。
//虚方法可以在不同的继承类中有不同的实现。
//对虚方法的调用是在运行时发生的。
//动态多态性是通过抽象类和虚方法实现的。
//以下实例创建了Shape基类,并创建派生类Circle、Rectangle、Triangle、Shape类提供了一个名为Draw的虚拟方法,在每个派生类中重写该方法以绘制该类指定 的形状。
/*
* 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 Rectangle: Shape
* {
* public override void Draw()
* {
* Console.WriteLine("画一个长方形");
* base.Draw();
* }
* }
* class Triangle: Shape
* {
* public override void Draw()
* {
* Console.WriteLine("画一个三角形");
* base.Draw();
* }
* }
* class Circle:Shape
* {
* public override void Draw()
* {
* Console.WriteLine("画一个圆形");
* base.Draw();
* }
* }
* class Program
* {
* static void Main(string[] args)
* {
* var shapes = new List<Shape>{new Rectangle(),new Triangle(),new Circle()};
* foreach(var shape in shapes)
* {
* shape.Draw();
* }
* Console.WriteLine("按下任意键退出。");
* Console.ReadKey();
* }
* }
*/
Console.ReadKey();
}
}
}