如何使用扩展class创建实例?
How to use an extended class to create an instance?
我正在尝试使用扩展 class 创建一个实例,但是我有点困惑。
我很难解释这类事情,但这是我正在尝试做的事情:
我有 test1.java 其中包含
private double value1;
private double value2;
private double value3;
和 getters 和 setters 全部。
我有 test2.java 扩展 test1.java.
我有一个新值
private double value4;
在test2.java中,我还有一个构造函数,其中包含以下内容
test1 t = new test1();
t.setValue3(5.5);
我还有一个 setter 和一个 getter 作为新值4。
现在我终于有了第三个 class, test3.java
我想使用它创建一些实例,但是我不知道如何使用 test2.java。
我所知道的是我可以用 test1.java 创建一个实例,但是我如何也包含 test2.java 东西以便每个实例的 value3 都为 5.5 并且还包含 value4?
如果 Test3 extends Test2
它将具有值 value4
。关于构造函数,你在 Test2
的构造函数
中的行
test1 t=new test1();
在你的 Test2
中创建一个新对象 Test1
object.I 认为你想在你的 Test2
中设置你的继承变量,这样做将构造函数重写为:
super();
this.setValue3(5.5);
函数super()
调用superclass构造函数,关键字this
引用class,在新的[=30=中是used.So ] Test3
只需在其构造函数中执行相同的操作即可将 value3
设置为 5.5
如果 test2.java
extends test1.java
,那么所有 public/protected
成员(具体的 getters 和 setters )都继承于 test2.java
现在 test3.java
你必须这样做
- 创建 Class
test2.java
的对象(比如 test2)
现在调用任意数量的 getter 和 setter。
test2.getValue1();
test2.getValue2();
test2.getValue3();
test2.setValue4(2.2);
and 在 test2.java
的构造函数中,您可以直接调用 setValue3(2.5)
而不是创建 class test1.java
的新对象
every instance will have value3 of 5.5
是 现在每当您创建 class test2.java
的对象时,您将得到 value3 as 5.5
我正在尝试使用扩展 class 创建一个实例,但是我有点困惑。
我很难解释这类事情,但这是我正在尝试做的事情:
我有 test1.java 其中包含
private double value1;
private double value2;
private double value3;
和 getters 和 setters 全部。
我有 test2.java 扩展 test1.java.
我有一个新值
private double value4;
在test2.java中,我还有一个构造函数,其中包含以下内容
test1 t = new test1();
t.setValue3(5.5);
我还有一个 setter 和一个 getter 作为新值4。
现在我终于有了第三个 class, test3.java
我想使用它创建一些实例,但是我不知道如何使用 test2.java。
我所知道的是我可以用 test1.java 创建一个实例,但是我如何也包含 test2.java 东西以便每个实例的 value3 都为 5.5 并且还包含 value4?
如果 Test3 extends Test2
它将具有值 value4
。关于构造函数,你在 Test2
的构造函数
test1 t=new test1();
在你的 Test2
中创建一个新对象 Test1
object.I 认为你想在你的 Test2
中设置你的继承变量,这样做将构造函数重写为:
super();
this.setValue3(5.5);
函数super()
调用superclass构造函数,关键字this
引用class,在新的[=30=中是used.So ] Test3
只需在其构造函数中执行相同的操作即可将 value3
设置为 5.5
如果 test2.java
extends test1.java
,那么所有 public/protected
成员(具体的 getters 和 setters )都继承于 test2.java
现在 test3.java
你必须这样做
- 创建 Class
test2.java
的对象(比如 test2)
现在调用任意数量的 getter 和 setter。
test2.getValue1();
test2.getValue2();
test2.getValue3();
test2.setValue4(2.2);
and 在 test2.java
的构造函数中,您可以直接调用 setValue3(2.5)
而不是创建 class test1.java
every instance will have value3 of 5.5
是 现在每当您创建 class test2.java
的对象时,您将得到 value3 as 5.5