当前位置:网站首页>C inheritance and interface design polymorphism
C inheritance and interface design polymorphism
2022-07-07 23:33:00 【Bobo in summer】
Hide base class : Law 1- Replace the base member with a new derived member
class A
{
public void fun()
{
Console.WriteLine("A");
}
}
class B : A
{
**new public void fun() // Hide base class methods fun**
{
Console.WriteLine("B");
}
}
static void Main(string[] args)
{
B b = new B();
b.fun();
}
// The result is “B”
Law 2- Rewrite the virtual base member
rewrite : Subclass base class has the same signature .: Overridden base method virtual、abstract or override.
heavy load : Of the same kind , Same name , Different parameters , Different signatures .
Default , Method is not virtual , You cannot override a non virtual method .virtual A modifier cannot be compared with static、abstract and override Use modifiers together . Use on static properties virtual The modifier is wrong .
class Student
{
protected int no; // Student number
protected string name; // full name
protected string tname; // Head teacher or instructor
public void setdata(int no1, string name1, string tname1)
{
no = no1; name = name1; tname = tname1;
}
public virtual void dispdata() // Virtual method
{
Console.WriteLine(" Undergraduate Student number :{0} full name :{1} class Lord ren :{2}", no, name, tname);
}
class Graduate : Student
{
public override void dispdata() // Rewriting methods
{
Console.WriteLine(" Graduate student Student number :{0} full name :{1} The instructor :{2}",no, name, tname);
}
}
static void Main(string[] args)
{
Student s = new Student();
s.setdata(101, " Wang Hua ", " Li Liang ");
s.dispdata();
Graduate g = new Graduate();
g.setdata(201, " Zhang hua ", " Chen Jun ");
g.dispdata();
}
Design a console application , Use the virtual method to find a rectangle 、 round 、 Area or surface area of sphere and cylinder .
public class Rectangle // Rectangle class
{
public const double PI = Math.PI;
protected double x, y;
public Rectangle() {
}
public Rectangle(double x1, double y1)
{
x = x1; y = y1;
}
public virtual double Area() // Find the area
{
return x * y;
}
public class Circle : Rectangle // Circles
{
public Circle(double r) : base(r, 0) {
}
public override double Area() // Find the area
{
return PI * x * x;
}
}
class Sphere : Rectangle // Spheroids
{
public Sphere(double r) : base(r, 0) {
}
public override double Area() // Find the area
{
return 4 * PI * x * x;
}
}
class Cylinder : Rectangle // Cylinder class
{
public Cylinder(double r, double h) : base(r, h) {
}
public override double Area() // Find the area
{
return 2 * PI * x * x + 2 * PI * x * y;
}
}
static void Main(string[] args)
{
double x = 2.4, y = 5.6;
double r = 3.0, h = 5.0;
Rectangle t = new Rectangle(x, y);
Rectangle c = new Circle(r);
Rectangle s = new Sphere(r);
Rectangle l = new Cylinder(r, h);
Console.WriteLine(" Long for {0}, Wide for {1} Rectangular area of ={2:F2}",
x, y, t.Area());
Console.WriteLine(" The radius is {0} Circular area of ={1:F2}",
r, c.Area());
Console.WriteLine(" The radius is {0} The surface area of the sphere ={1:F2}",
r, s.Area());
Console.WriteLine(" The radius is {0}, The height is {1} Surface area of cylinder ={2:F2}", r, h, l.Area());
}
边栏推荐
- 包装行业智能供应链S2B2B商城解决方案:开辟电商消费新生态
- 欢聚时代一面
- Vs extension tool notes
- 0-1背包问题
- 进度播报|广州地铁七号线全线29台盾构机全部完成始发
- UE4_ Ue5 combined with Logitech handle (F710) use record
- Force deduction solution summary 648 word replacement
- 2022第六季完美童模陕西总决赛圆满落幕
- POJ2392 SpaceElevator [DP]
- [stm32+esp8266 connect Tencent cloud IOT development platform 2] stm32+esp8266-01s connect Tencent cloud
猜你喜欢
MySQL Index Optimization Practice II
云原生正在吞噬一切,开发者该如何应对?
Understand TCP's three handshakes and four waves with love
移动端异构运算技术 - GPU OpenCL 编程(基础篇)
城联优品作为新力量初注入,相关上市公司股价应声上涨150%
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
How to change the formula picture in the paper directly into the formula in word
Right click the idea file to create new. There is no solution to create new servlet
B / Qurt Utilisateur Guide (36)
leetcode-520. Detect capital letters -js
随机推荐
SAP HR奖罚信息导出
Coreseek: the second step is index building and testing
How to change the formula picture in the paper directly into the formula in word
PCB wiring rules of PCI Express interface
B_QuRT_User_Guide(37)
SAP HR 家庭成员信息
B_ QuRT_ User_ Guide(37)
USB (XV) 2022-04-14
LM12丨Rolling Heikin Ashi二重K线滤波器
三问TDM
做自媒体视频剪辑怎么赚钱呢?
统计电影票房排名前10的电影并存入还有一个文件
Right click the idea file to create new. There is no solution to create new servlet
LeeCode -- 6. Zigzag transformation
Markdown
JS get the key and value of the object
[untitled]
Explain
深入理解Mysql锁与事务隔离级别
Extended tree (I) - graphic analysis and C language implementation