Tag: inheritance

从Vectorinheritance的Java类Stack的负面影响是什么?

通过扩展类Vector,Java的设计者能够快速创建类Stack。 这种inheritance使用的负面影响是什么,特别是对于类Stack? 非常感谢。

需要Java代码段输出说明

我的代码是: class Foo { public int a=3; public void addFive() { a+=5; System.out.print(“f “); } } class Bar extends Foo { public int a=8; public void addFive() { this.a += 5; System.out.print(“b “); } } public class TestClass { public static void main(String[]args) { Foo f = new Bar(); f.addFive(); System.out.println(fa); } } 输出: b […]

在inheritance的类中使用私有变量 – Java

需要对私有变量和inheritance有更多的了解。 之前我的理解是,如果一个类中有字段,并且当我inheritance该类时,不受访问限制的字段(私有变量)将存在于inheritance的类中。 但是如果有一个公共g / setter方法,我可以在基类中使用私有变量。 我怎样才能想象一个基类中的私有变量。

基础构造函数的Java调用基方法

如何从Super :: Super()调用Super :: printThree? 在下面的例子中,我改为调用Test :: printThree。 class Super { Super() { printThree(); // I want Super::printThree here! } void printThree() { System.out.println(“three”); } } class Test extends Super { int three = 3 public static void main(String[] args) { Test t = new Test(); t.printThree(); } void printThree() { System.out.println(three); } } output: […]

静态方法中的inheritance

为什么以下代码打印“Main”? public class Main { public static void method() { System.out.println(“Main”); } public static void main(String[] args) { Main m = new SubMain(); m.method(); } } class SubMain extends Main { public static void method() { System.out.println(“SubMain”); } } 在运行时, m指向Submain一个实例,因此它应该在概念上打印“SubMain”。

当Base类构造函数在Java中调用重写方法时,Derived类对象的状态

请参考下面的Java代码: class Base{ Base(){ System.out.println(“Base Constructor”); method(); } void method(){} } class Derived extends Base{ int var = 2; Derived(){ System.out.println(“Derived Constructor”); } @Override void method(){ System.out.println(“var = “+var); } } class Test2{ public static void main(String[] args) { Derived b = new Derived(); } } 看到的输出是: Base Constructor var = 0 Derived Constructor 我认为var […]

Java:当A扩展A 时,A x = new A()和A x = new B()之间的差异

可能重复: javainheritance – 请解释 我正在学习Java,我有两个问题: 有什么区别: A x = new A(); 和 A x = new B(); 考虑到: class A class B extends A 有什么区别: A x = new B(); (A)x.run_function(); 假设A和B都有run_function函数,哪一个会被执行?

可以在子类中重写超类中的私有方法吗?

可以在Java中覆盖私有方法吗? 如果不是,那么以下代码如何工作? class Base{ private void func(){ System.out.println(“In Base Class func method !!”); }; } class Derived extends Base{ public void func(){ // Is this a Method Overriding..???? System.out.println(“In Derived Class func method”); } } class InheritDemo{ public static void main(String [] args){ Derived d = new Derived(); d.func(); } }

类不是抽象的,不会覆盖抽象方法

所以我一直在为编程课做抽象作业,但遇到了问题。 我现在的目标是能够使用抽象,然后能够用矩形和椭圆形绘制一个简单的城市,如矩形建筑物或灯柱上的椭圆形灯。 我编译时收到的错误是:MyTestApp.Rectangle不是抽象的,并且不会覆盖MyTestApp.Shape中的抽象方法drawEllipse(java.awt.Graphics)。 此错误显示在类Shape下方的“类Rectangle extends Shape {”行中。 我的问题是我的抽象错误是什么? 我一直在搞乱Rectangle和Ellipse类中的构造函数和draw()方法一段时间,但仍然没有找到解决方案的运气。 代码如下: import java.awt.*; import javax.swing.*; public class MyTestApp extends JPanel { Rectangle rect; Ellipse oval; public static void main(String [] args) { MyTestApp myTestApp = new MyTestApp (); myTestApp.test(); } public MyTestApp () { //creates the jframe JFrame frame = new JFrame(“MyClass Driver”); setBackground(new Color(200, 250, […]

是否inheritance了静态变量

我已经读过1000个位置的静态变量没有被inheritance。 但那么这段代码如何正常工作呢? Parent.java public class Parent { static String str = “Parent”; } Child.java public class Child extends Parent { public static void main(String [] args) { System.out.println(Child.str); } } 此代码打印“父”。 还可以在几个位置读取数据隐藏的概念。 Parent.java public class Parent { static String str = “Parent”; } Child.java public class Child extends Parent { static String str = “Child”; […]