英文:
Multiple child class vs Single child class - Java Inheritance
问题
I have a question that extending a Parent class to multiple child classes is a good practice or maintaining a single child is a good practice? Does this affect scalability? All the classes will be executed at the same time. So I have this question.
例如,我只使用了4个类。在实际情况下,将有83个类使用相同的方法并同时执行。
public interface A
{
methodA();
}
public class B implements A
{
methodA()
{
.....
}
}
public class C
{
B b = new B();
b.methodA();
}
public class D
{
B b = new B();
b.methodA();
}
或者
public interface A
{
methodA();
}
public class B implements A
{
methodA()
{
.....
}
}
public class C extends B
{
super.methodA();
}
public class D extends B
{
super.methodA();
}
或者
public interface A
{
methodA();
}
public class B implements A
{
methodA()
{
.....
}
}
public class C extends B
{
super.methodA();
}
public class D extends C
{
super.methodA();
}
英文:
I have a question that extending a Parent class to multiple child classes is a good practice or maintaining a single child is a good practice? Does this affects the scalability? All the classes will be executed at the same time. So I have this question.
for example I have used only 4 classes. In real time It will be like 83 classes will be using the same method and gets executed at once.
public interface A
{
methodA();
}
public class B implements A
{
methodA()
{
.....
}
}
public class C
{
B b = new B();
b.methodA();
}
public class D
{
B b = new B();
b.methodA();
}
Or
public interface A
{
methodA();
}
public class B implements A
{
methodA()
{
.....
}
}
public class C extends B
{
super.methodA();
}
public class D extends B
{
super.methodA();
}
Or
public interface A
{
methodA();
}
public class B implements A
{
methodA()
{
.....
}
}
public class C extends B
{
super.methodA();
}
public class D extends C
{
super.methodA();
}
答案1
得分: 1
遵循 SOLID 原则。这些设计原则鼓励我们创建更易维护、易理解和灵活的软件。因此,随着我们的应用程序规模的增长,我们可以降低其复杂性,从而在未来避免许多麻烦!
英文:
Go through the SOLID principle. Its a design principles encourage us to create more maintainable, understandable, and flexible software. Consequently, as our applications grow in size, we can reduce their complexity and save ourselves a lot of headaches further down the road!
答案2
得分: 0
你可以使用 private 或 default 访问修饰符来定义 methodA 的代码。
课程:接口和继承(Java™ 教程 > 学习 Java 语言)。
英文:
You could define the code for methodA if you use the private or default access modifier.
Lesson: Interfaces and Inheritance (The Java™ Tutorials > Learning the Java Language).
interface A {
private String method() {
return "stack overflow";
}
}
class B implements A {
String string = A.super.method();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论