Tag: 超级

Java设置实例的超级实例

我可能只是无法谷歌正确的单词,但我找不到以下问题的答案。 是否可以显式设置新类实例的超类。 例如,我有一个SuperClazz实例,想要创建一个新的SuperClazz实例,它扩展了SuperClazz 。 我可以做这样的事情(代码就是我想要做的,它不能编译,也不正确): class Clazz extends SuperClazz{ Clazz(SuperClazz superInstance){ this.super = superInstance; } }

Java – 使用’super’关键字

简单的问题。 我创建了一个名为Tester1的类,它扩展了另一个名为Tester2的类。 Tester2包含一个名为’ABC’的公共字符串。 这是Tester1: public class Tester1 extends Tester2 { public Tester1() { ABC = “Hello”; } } 如果我改为第5行 super.ABC = “Hello”; 我还在做同样的事吗?

在Java类型参数中,仅指严格的亚型? 还是E也足够了?

在Java类型参数中,是否仅指严格的子类型? 还是E也足够了?

Java通配符和generics? 超级T和? 延伸T.

当处理通配符,如设置/添加通用项目到某个容器时,是否建议使用这样的东西? void add(List someList,someitem){ someList.add(someItem); } 当检索项目时,建议使用这样的东西 void f1(List obj, T item) { obj.add(item); } 这背后的原理是什么? 我什么时候才能知道我是否应该使用它?

如果在构造函数中使用super调用重写方法会发生什么

有两个类Super1和Sub1 Super1.class public class Super1 { Super1 (){ this.printThree(); } public void printThree(){ System.out.println(“Print Three”); } } Sub1.class public class Sub1 extends Super1 { Sub1 (){ super.printThree(); } int three=(int) Math.PI; public void printThree(){ System.out.println(three); } public static void main(String …a){ new Sub1().printThree(); } } 当我调用类Sub1的方法printThree ,我期望输出为: 打印三 3 因为Sub1构造函数调用了super.printThree(); 。 但我真的得到了 0 打印三 3 […]

调用super必须是构造函数中的第一个语句,但它是

我不断收到一条错误,说“调用super必须是构造函数中的第一个语句”。 问题是它是我构造函数中的第一个语句。 public void CheckingAccountCustomer(int a){ super(n, p, b); accountNo = a; } 这也是我的超类。 public void customer(String n, int p, double b){ name = n; pin = p; balance = b; } 我在这做错了什么?

Java中super关键字的范围和用法

为什么我不能用super关键字访问父类变量? 使用以下代码,输出为: feline cougar cc class Feline { public String type = “f “; public Feline() { System.out.print(“feline “); } } public class Cougar extends Feline { public Cougar() { System.out.print(“cougar “); } void go() { type = “c “; System.out.print(this.type + super.type); } public static void main(String[] args) { new Cougar().go(); } }

如何访问Abstract超类实例变量

所以我有两个class: Property和Houses 。 Property是抽象超类,而Houses是它的子类。 这是Property的代码 public abstract class Property{ String pCode; double value; int year; public Property(String pCode, double value , int year){ this.pCode = pCode; this.value = value; this.year = year; } public Property(){ pCode = “”; value = 0; year = 0; } public abstract void depreciation(); //Accessors private String getCode(){ return pCode; […]

如何在此(…)或超级(…)之前“插入”代码?

有没有办法在调用super(…)或this(…)构造函数之前实现初步计算? 请考虑以下示例: public class Test { private final int n; private final int m; private final int[] store; public Test(int n, int m) { /* This is common (most generic) constructor of the class Test. It is desirable to invoke it via this(…) call from any other constructor of this class since it contains some […]

可以在超级构造函数完成之前初始化java子类的私有final字段吗?

我有一对看起来像这样的课程; public abstract class Class1 { //… public Class1() { //… function2(); //… } protected abstract void function2(); } public class Class2 implements Class1 { private final OnSomethingListener mOnSomethingListener = new OnSomethingListener() { @Override onSomething() { doThatOtherThing(); } } protected void function2() { //uses mOnSomethingListener //however mOnSomethingListener is null when this function is called from […]