当前位置:网站首页>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());
}
边栏推荐
- SQL database execution problems
- MATLAB signal processing [Q & A essays · 2]
- Matlab SEIR infectious disease model prediction
- Explain
- SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
- 2022 Season 6 perfect children's model Shaanxi finals came to a successful conclusion
- Markdown
- Vs extension tool notes
- 云原生数据仓库AnalyticDB MySQL版用户手册
- How to generate unique file names
猜你喜欢
2022注册测绘师备考开始 还在不知所措?手把手教你怎么考?
ROS2专题(03):ROS1和ROS2的区别【01】
产业共融新势能,城链科技数字峰会厦门站成功举办
伸展树(一) - 图文解析与C语言实现
First week of July
How to change the formula picture in the paper directly into the formula in word
[compilation principle] lexical analysis design and Implementation
Cloud native is devouring everything. How should developers deal with it?
Understand TCP's three handshakes and four waves with love
Mysql索引优化实战一
随机推荐
【7.5】15. 三数之和
Summary of common methods of object class (September 14, 2020)
Experience sharing of system architecture designers in preparing for the exam: the direction of paper writing
移动端异构运算技术 - GPU OpenCL 编程(基础篇)
Tree background data storage (using webmethod) [easy to understand]
RE1 attack and defense world reverse
Entity层、DAO层、Service层、Controller层 先后顺序
PHP uses Alibaba cloud storage
Sequence of entity layer, Dao layer, service layer and controller layer
2021icpc Shanghai h.life is a game Kruskal reconstruction tree
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
Markdown
re1攻防世界逆向
Markdown
SAP HR 劳动合同信息 0016
Home appliance industry channel business collaboration system solution: help home appliance enterprises quickly realize the Internet of channels
Coreseek:第二步建索引及測试
进度播报|广州地铁七号线全线29台盾构机全部完成始发
System design overview
KeePass realizes automatic input of web pages