当这个 class 扩展另一个 class 时,class 可以为空(没有任何参数)吗?
Can class be empty (without any parametr) when this class extend another class?
namespace FinalExam
{
class Developer
{
string name;
public void sayGoodMorning()
{
Console.Write("Good Morning! ");
}
public void sayHi()
{
Console.WriteLine("Hi!");
}
public void startDiscussion()
{
Console.WriteLine("How is going your work?");
}
public void setName(string a)
{
name = a;
}
public void getName()
{
Console.WriteLine(name);
}
}
class BackEnd : Developer
{
}
class FrontEnd : Developer
{
}
class FrontEndBackEndTest
{
static void Main(string[] args)
{
Developer Zaur = new Developer();
Zaur.sayGoodMorning();
Console.WriteLine();
BackEnd backEnd = new BackEnd();
backEnd.setName("Ben");
backEnd.sayGoodMorning();
backEnd.getName();
Console.WriteLine();
FrontEnd frontEnd = new FrontEnd();
frontEnd.setName("Jane");
frontEnd.sayHi();
frontEnd.startDiscussion();
}
}
}
我的 class 是这样的(不要介意名称或调用对象只是为了举例)在这个例子中必须在子 class 中写一些东西或者可以是空的?
是的,扩展另一个 class 的 class 可以是空的。
你的代码编译并运行,因此被 c# 编译器允许。
但是,当您可以将 backEnd
设为 Developer
本身的实例时,拥有一个空的 class 似乎毫无意义(实际上,我知道这只是一个示例)除非有些点你需要通过类型来区分它们(即 backEnd.GetType()
)。
namespace FinalExam
{
class Developer
{
string name;
public void sayGoodMorning()
{
Console.Write("Good Morning! ");
}
public void sayHi()
{
Console.WriteLine("Hi!");
}
public void startDiscussion()
{
Console.WriteLine("How is going your work?");
}
public void setName(string a)
{
name = a;
}
public void getName()
{
Console.WriteLine(name);
}
}
class BackEnd : Developer
{
}
class FrontEnd : Developer
{
}
class FrontEndBackEndTest
{
static void Main(string[] args)
{
Developer Zaur = new Developer();
Zaur.sayGoodMorning();
Console.WriteLine();
BackEnd backEnd = new BackEnd();
backEnd.setName("Ben");
backEnd.sayGoodMorning();
backEnd.getName();
Console.WriteLine();
FrontEnd frontEnd = new FrontEnd();
frontEnd.setName("Jane");
frontEnd.sayHi();
frontEnd.startDiscussion();
}
}
}
我的 class 是这样的(不要介意名称或调用对象只是为了举例)在这个例子中必须在子 class 中写一些东西或者可以是空的?
是的,扩展另一个 class 的 class 可以是空的。
你的代码编译并运行,因此被 c# 编译器允许。
但是,当您可以将 backEnd
设为 Developer
本身的实例时,拥有一个空的 class 似乎毫无意义(实际上,我知道这只是一个示例)除非有些点你需要通过类型来区分它们(即 backEnd.GetType()
)。