当前位置:网站首页>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());
}
边栏推荐
- 产业共融新势能,城链科技数字峰会厦门站成功举办
- Progress broadcast | all 29 shield machines of Guangzhou Metro Line 7 have been launched
- 系统设计概述
- Force deduction solution summary 648 word replacement
- One week learning summary of STL Standard Template Library
- SAP HR 社会工作经历 0023
- 0-1背包问题
- IDEA 2021.3. X cracking
- [untitled]
- 在软件工程领域,搞科研的这十年!
猜你喜欢
Spark 离线开发框架设计与实现
Right click the idea file to create new. There is no solution to create new servlet
Design and implementation of spark offline development framework
Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process
Installing spss25
进度播报|广州地铁七号线全线29台盾构机全部完成始发
在软件工程领域,搞科研的这十年!
[stm32+esp8266 connect Tencent cloud IOT development platform 2] stm32+esp8266-01s connect Tencent cloud
LM12丨Rolling Heikin Ashi二重K线滤波器
How to change the formula picture in the paper directly into the formula in word
随机推荐
Unity3d learning notes 4 - create mesh advanced interface
城联优品作为新力量初注入,相关上市公司股价应声上涨150%
电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
2021icpc Shanghai h.life is a game Kruskal reconstruction tree
Coreseek: the second step is index building and testing
2022 certified surveyors are still at a loss when preparing for the exam? Teach you how to take the exam hand in hand?
Have all the fresh students of 2022 found jobs? Is it OK to be we media?
The 19th Zhejiang Provincial College Programming Contest 2022 f.easyfix chairman tree
FreeLink开源呼叫中心设计思想
Ros2 topic (03): the difference between ros1 and ros2 [02]
Unity3d learning notes 5 - create sub mesh
Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
ESP at installation esp8266 and esp32 versions
New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
ROS2专题(03):ROS1和ROS2的区别【02】
Vs extension tool notes
LM12丨Rolling Heikin Ashi二重K线滤波器
windows设置redis开启自动启动
SQL database execution problems
Markdown