Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在面向对象理论中,派生类应该继承父对象吗;用户界面?_Java_C#_Inheritance_Interface - Fatal编程技术网

Java 在面向对象理论中,派生类应该继承父对象吗;用户界面?

Java 在面向对象理论中,派生类应该继承父对象吗;用户界面?,java,c#,inheritance,interface,Java,C#,Inheritance,Interface,我是一个自学成才的业余程序员,我所有的知识都来源于看到编译器喜欢什么和不喜欢什么 假设我有(在C#表示法中,Java可能有其他能力) 我需要重写的类如下所示: public interface ICanDoSomethingElse { void DoIt(); } class Parent : ICanDoSomethingElse { public void EatTacos() { } void ICanDoSomethingElse.DoIt(strin

我是一个自学成才的业余程序员,我所有的知识都来源于看到编译器喜欢什么和不喜欢什么

假设我有(在C#表示法中,Java可能有其他能力)

我需要重写的类如下所示:

public interface ICanDoSomethingElse
{
    void DoIt();
}
class Parent : ICanDoSomethingElse
{
   public void EatTacos()
   {
   }

   void ICanDoSomethingElse.DoIt(string thingToDo)
   {
      // Implementation
   }
}
class Parent : ICanDoSomethingElse
{
   ...

   public void DoIt(string thingToDo)
   {
      // Implementation
   }
}

class Child : Parent, ICanDoSomethingElse
{
   ...

   public new void DoIt(string thingToDo)
   {
      // Implementation
   }
}
所以我这样做:

class Child : Parent, ICanDoSomethingElse
{
   new public void EatTacos()
   {
   }
   void ICanDoSomethingElse.DoIt(string thingToDo) // NEW keyword illegal here? 
   {
      // Implementation
   }
}
问题

我发现新的关键字在界面中是非法的。这是因为这是一个显式接口吗


有没有办法强迫孩子们实现接口,或者这仅仅意味着我需要设置隐式/显式转换

我认为您正在执行的
接口
实现与
新的
关键字无关

在从父方法继承的方法上定义新行为时,使用
new
关键字

public class Parent{
    public void SayHello(){
        Console.WriteLine("Hello from parent");
    }    
}
那孩子呢

public class Child : Parent {
    public new void SayHello(){
        Console.WriteLine("Hello from child");
    }    
}
如果要将接口用作实现,则不需要使用
new
关键字

我注意到新关键字在界面中是非法的。这是因为这是一个显式接口吗

错误是因为您在类中显式实现接口

显式实现仅当您直接使用接口而不是底层实现时,才强制公开方法

如果您的课程是这样的:

public interface ICanDoSomethingElse
{
    void DoIt();
}
class Parent : ICanDoSomethingElse
{
   public void EatTacos()
   {
   }

   void ICanDoSomethingElse.DoIt(string thingToDo)
   {
      // Implementation
   }
}
class Parent : ICanDoSomethingElse
{
   ...

   public void DoIt(string thingToDo)
   {
      // Implementation
   }
}

class Child : Parent, ICanDoSomethingElse
{
   ...

   public new void DoIt(string thingToDo)
   {
      // Implementation
   }
}
您需要使用
new
关键字以某种方式隐藏
Parent.DoIt
实现

有没有办法强迫孩子们实现接口,或者这仅仅意味着我需要设置隐式/显式转换

您可以将
Parent
class
abstract
,然后将接口的方法
abstract

abstract class Parent : ICanDoSomethingElse
{
   ...

   public abstract void DoIt(string thingToDo);
}

class Child : Parent, ICanDoSomethingElse
{
   ...

   public override void DoIt(string thingToDo)
   {
      // Implementation
   }
}

这里,,
Child
必须实现
DoIt
方法。

像这样的语言设计问题可能更适合cs.stackexchange.com。我认为
Child
应该继承自
Parent
这段代码在任何年龄段都没有父子关系或基础派生关系,这并不意味着senseI看不到任何关系Java没有任何与C#的
new
method修饰符等价的东西。我想我要问的是,为什么我不理解,看看这个答案