Java 继承:如何从 parent class 覆盖实例 variables/fields?
Java Inheritance: How to override instance variables/fields from parent class?
更新:我可以将变量从私有、静态或最终更改。
我有一个parentclass和一个childclass。我想re-use中的一个方法parentclass。通常,这就像 super.methodFromParentClass()
一样简单,您就完成了。 但是,当我这样做的时候,我想要re-use的方法是using实例来自 Parent class 的可变数据,这是 错误的 或者更确切地说,我 而不是 想要这种行为。我在 childclass 中有 DIFFERENT 初始化数据,需要将其传递到我要重用的方法中。如果你看一下我想要 re-use 的方法(下面只是一个简单的例子,但想法是一样的),我正在创建多个 objects 在那里使用实例变量class 它被调用了。所以你可以看到为什么当我调用 super.methodIWantToReuse
时它不起作用,因为它将获取 Parent 数据并将其传递到 objects ,尽管我真的希望它传递我在childclass中初始化的数据。我的真实示例还创建了更多 objects 并且我有更多的实例变量,所以我真的很想 re-use 如果可能的话,这段代码(DRY 原则)。
我该如何解决这个问题?将使用 getters 即 getFirstName()
并在 Child class 中覆盖它们,因此当使用 Runtime Polymorphism我调用super.methodIWantToReuse()
,grab/use Child class 实例变量数据是唯一的方法吗???
public class ParentClass {
private static final String firstName = "Billy Ray";
private static final String lastName = "Cyrus";
private static final int age = 58;
private static final String city = "Hunstville";
public boolean methodIWantToReuse() {
Object1 obj1 = new Object(firstName, lastName);
Object2 obj2 = new Object(age,city);
Object3 obj3 = new Object(obj1, obj2);
Object4 obj4 = new Object(obj3);
// Passing in the objects created above as argument, which have the Parent instance variable data
return someRandomMethodHere(obj4);
}
public class ChildClass {
private static final String firstName = "Miley";
private static final String lastName = "Cyrus";
private static final int age = 27;
private static final String city = "Los Angeles";
public boolean methodIWantToReuse() {
// DOESN'T WORK CORRECTLY, because ends up using the instance variable data of PARENT class, but it
// needs to use CHILD class instance variable data
super.methodIWantToReuse();
}
您不能覆盖 class 的字段。只有方法可以被覆盖。在您的情况下,您必须使用吸气剂并在子 class.
中覆盖它们
如果您的意思是 instance variables
而不是示例中所示的 static variables
(或 class variables
),您可以让子 class 可以访问它们通过更改访问修饰符并删除 final
关键字。
但是,如果您实际上是指 static variables
,则不能在每个子 class 中重新分配它们,因为它们将共享 ParentClass
定义的相同静态变量,这意味着最后加载 class 将是您通过调用 ParentClass#methodIWantToReuse
.
获得的唯一结果
最好的方法是使用 OOP,通过使用所需参数实例化新的单个对象并使用它们来发挥自己的优势。
我的意思是不是这样做:
public class Example {
public static class ParentClass {
protected String name;
protected int age;
public ParentClass() {
name = "The parent";
age = 35;
}
public String methodIWantToReuse() {
return name + " is " + age + " years old.";
}
}
public static class AChildClass extends ParentClass {
public AChildClass() {
name = "Alice";
age = 13;
}
}
public static class AnotherChildClass extends ParentClass {
public AnotherChildClass() {
name = "Bob";
age = 21;
}
}
public static void main(String[] args) {
// Prints "The parent is 35 years old."
System.out.println(new ParentClass().methodIWantToReuse());
// Prints "Alice is 13 years old."
System.out.println(new AChildClass().methodIWantToReuse());
// Prints "Bob is 21 years old."
System.out.println(new AnotherChildClass().methodIWantToReuse());
}
}
这样做:
public class Example {
public static class ParentClass {
protected String name;
protected int age;
// Variables instantiated here to not cause confusion
public ParentClass() {
name = "The parent";
age = 35;
}
public String methodIWantToReuse() {
return name + " is " + age + " years old.";
}
}
public static class ChildClass extends ParentClass {
public ChildClass(String name, int age) {
this.name = name;
this.age = age;
}
}
public static void main(String[] args) {
// Prints "The parent is 35 years old."
System.out.println(new ParentClass().methodIWantToReuse());
// Prints "Alice is 13 years old."
System.out.println(new ChildClass("Alice", 13).methodIWantToReuse());
// Prints "Bob is 21 years old."
System.out.println(new ChildClass("Bob", 21).methodIWantToReuse());
}
}
这也应该遵循 DRY 原则,因为您希望尽可能高效地重用代码,而不是一遍又一遍地编写相同的技术代码。
如您所见,我不需要重写 ParentClass#methodIWantToReuse
或调用 ChildClass
' super 的实现。
你的 parent class 实例变量是私有的,所以你不能从 Child class 更新它们。因此,您可以使用参数化方法或为实例变量(或受保护变量本身)创建 Protected setter/getter 。在你的情况下,变量是最终的,所以你实际上什至不能更新它们。所以从技术上讲,不可能在 parent class.
中使用 child class 变量
如果您将变量更新为 protected
并删除 static/final
修饰符(正如您在评论中提到的那样)。在从 parent class 调用方法之前,在调用超级方法之前更新变量数据。您可以按以下方式进行:
方法 1: 在调用 parent class 方法之前更新 parent class 中的数据。
Parent Class:
public class ParentClass {
protected String firstName = "Billy Ray";
protected String lastName = "Cyrus";
protected int age = 58;
protected String city = "Hunstville";
public boolean methodIWantToReuse() {
// Passing in the objects created above as argument, which have the Parent
// instance variable data
Object1 obj1 = new Object(firstName, lastName);
Object2 obj2 = new Object(age,city);
Object3 obj3 = new Object(obj1, obj2);
Object4 obj4 = new Object(obj3);
return someRandomMethodHere(obj4);;
}
}
Child Class:
public class ChildClass extends ParentClass {
protected String firstName = "Miley";
protected String lastName = "Cyrus";
protected int age = 27;
protected String city = "Los Angeles";
public boolean methodIWantToReuse() {
// Update data in Parent class first
super.firstName = firstName;
super.lastName = lastName;
super.age = age;
super.city = city;
return super.methodIWantToReuse();
}
}
方法二:如果你想使用参数化的方法让它成为无状态的,你可以这样做:
Parent Class:
public class ParentClass {
protected String firstName = "Billy Ray";
protected String lastName = "Cyrus";
protected int age = 58;
protected String city = "Hunstville";
public boolean methodIWantToReuse() {
return methodIWantToReuse(this.firstName, this.lastName, this.age, this.city);
}
public boolean methodIWantToReuse(String firstName, String lastName, int age, String city) {
// Passing in the objects created above as argument, which have the Parent
// instance variable data
Object1 obj1 = new Object(firstName, lastName);
Object2 obj2 = new Object(age,city);
Object3 obj3 = new Object(obj1, obj2);
Object4 obj4 = new Object(obj3);
return someRandomMethodHere(obj4);;
}
}
Child Class:
public class ChildClass extends ParentClass {
protected String firstName = "Miley";
protected String lastName = "Cyrus";
protected int age = 27;
protected String city = "Los Angeles";
public boolean methodIWantToReuse() {
// Update data in Parent class first
return super.methodIWantToReuse(this.firstName, this.lastName, this.age, this.city);
}
}
注意: 保持局部变量名称与 class 级别变量相同不是好的做法。但在这里保持不变只是为了理解。
更新:我可以将变量从私有、静态或最终更改。
我有一个parentclass和一个childclass。我想re-use中的一个方法parentclass。通常,这就像 super.methodFromParentClass()
一样简单,您就完成了。 但是,当我这样做的时候,我想要re-use的方法是using实例来自 Parent class 的可变数据,这是 错误的 或者更确切地说,我 而不是 想要这种行为。我在 childclass 中有 DIFFERENT 初始化数据,需要将其传递到我要重用的方法中。如果你看一下我想要 re-use 的方法(下面只是一个简单的例子,但想法是一样的),我正在创建多个 objects 在那里使用实例变量class 它被调用了。所以你可以看到为什么当我调用 super.methodIWantToReuse
时它不起作用,因为它将获取 Parent 数据并将其传递到 objects ,尽管我真的希望它传递我在childclass中初始化的数据。我的真实示例还创建了更多 objects 并且我有更多的实例变量,所以我真的很想 re-use 如果可能的话,这段代码(DRY 原则)。
我该如何解决这个问题?将使用 getters 即 getFirstName()
并在 Child class 中覆盖它们,因此当使用 Runtime Polymorphism我调用super.methodIWantToReuse()
,grab/use Child class 实例变量数据是唯一的方法吗???
public class ParentClass {
private static final String firstName = "Billy Ray";
private static final String lastName = "Cyrus";
private static final int age = 58;
private static final String city = "Hunstville";
public boolean methodIWantToReuse() {
Object1 obj1 = new Object(firstName, lastName);
Object2 obj2 = new Object(age,city);
Object3 obj3 = new Object(obj1, obj2);
Object4 obj4 = new Object(obj3);
// Passing in the objects created above as argument, which have the Parent instance variable data
return someRandomMethodHere(obj4);
}
public class ChildClass {
private static final String firstName = "Miley";
private static final String lastName = "Cyrus";
private static final int age = 27;
private static final String city = "Los Angeles";
public boolean methodIWantToReuse() {
// DOESN'T WORK CORRECTLY, because ends up using the instance variable data of PARENT class, but it
// needs to use CHILD class instance variable data
super.methodIWantToReuse();
}
您不能覆盖 class 的字段。只有方法可以被覆盖。在您的情况下,您必须使用吸气剂并在子 class.
中覆盖它们如果您的意思是 instance variables
而不是示例中所示的 static variables
(或 class variables
),您可以让子 class 可以访问它们通过更改访问修饰符并删除 final
关键字。
但是,如果您实际上是指 static variables
,则不能在每个子 class 中重新分配它们,因为它们将共享 ParentClass
定义的相同静态变量,这意味着最后加载 class 将是您通过调用 ParentClass#methodIWantToReuse
.
最好的方法是使用 OOP,通过使用所需参数实例化新的单个对象并使用它们来发挥自己的优势。
我的意思是不是这样做:
public class Example {
public static class ParentClass {
protected String name;
protected int age;
public ParentClass() {
name = "The parent";
age = 35;
}
public String methodIWantToReuse() {
return name + " is " + age + " years old.";
}
}
public static class AChildClass extends ParentClass {
public AChildClass() {
name = "Alice";
age = 13;
}
}
public static class AnotherChildClass extends ParentClass {
public AnotherChildClass() {
name = "Bob";
age = 21;
}
}
public static void main(String[] args) {
// Prints "The parent is 35 years old."
System.out.println(new ParentClass().methodIWantToReuse());
// Prints "Alice is 13 years old."
System.out.println(new AChildClass().methodIWantToReuse());
// Prints "Bob is 21 years old."
System.out.println(new AnotherChildClass().methodIWantToReuse());
}
}
这样做:
public class Example {
public static class ParentClass {
protected String name;
protected int age;
// Variables instantiated here to not cause confusion
public ParentClass() {
name = "The parent";
age = 35;
}
public String methodIWantToReuse() {
return name + " is " + age + " years old.";
}
}
public static class ChildClass extends ParentClass {
public ChildClass(String name, int age) {
this.name = name;
this.age = age;
}
}
public static void main(String[] args) {
// Prints "The parent is 35 years old."
System.out.println(new ParentClass().methodIWantToReuse());
// Prints "Alice is 13 years old."
System.out.println(new ChildClass("Alice", 13).methodIWantToReuse());
// Prints "Bob is 21 years old."
System.out.println(new ChildClass("Bob", 21).methodIWantToReuse());
}
}
这也应该遵循 DRY 原则,因为您希望尽可能高效地重用代码,而不是一遍又一遍地编写相同的技术代码。
如您所见,我不需要重写 ParentClass#methodIWantToReuse
或调用 ChildClass
' super 的实现。
你的 parent class 实例变量是私有的,所以你不能从 Child class 更新它们。因此,您可以使用参数化方法或为实例变量(或受保护变量本身)创建 Protected setter/getter 。在你的情况下,变量是最终的,所以你实际上什至不能更新它们。所以从技术上讲,不可能在 parent class.
中使用 child class 变量如果您将变量更新为 protected
并删除 static/final
修饰符(正如您在评论中提到的那样)。在从 parent class 调用方法之前,在调用超级方法之前更新变量数据。您可以按以下方式进行:
方法 1: 在调用 parent class 方法之前更新 parent class 中的数据。
Parent Class:
public class ParentClass {
protected String firstName = "Billy Ray";
protected String lastName = "Cyrus";
protected int age = 58;
protected String city = "Hunstville";
public boolean methodIWantToReuse() {
// Passing in the objects created above as argument, which have the Parent
// instance variable data
Object1 obj1 = new Object(firstName, lastName);
Object2 obj2 = new Object(age,city);
Object3 obj3 = new Object(obj1, obj2);
Object4 obj4 = new Object(obj3);
return someRandomMethodHere(obj4);;
}
}
Child Class:
public class ChildClass extends ParentClass {
protected String firstName = "Miley";
protected String lastName = "Cyrus";
protected int age = 27;
protected String city = "Los Angeles";
public boolean methodIWantToReuse() {
// Update data in Parent class first
super.firstName = firstName;
super.lastName = lastName;
super.age = age;
super.city = city;
return super.methodIWantToReuse();
}
}
方法二:如果你想使用参数化的方法让它成为无状态的,你可以这样做:
Parent Class:
public class ParentClass {
protected String firstName = "Billy Ray";
protected String lastName = "Cyrus";
protected int age = 58;
protected String city = "Hunstville";
public boolean methodIWantToReuse() {
return methodIWantToReuse(this.firstName, this.lastName, this.age, this.city);
}
public boolean methodIWantToReuse(String firstName, String lastName, int age, String city) {
// Passing in the objects created above as argument, which have the Parent
// instance variable data
Object1 obj1 = new Object(firstName, lastName);
Object2 obj2 = new Object(age,city);
Object3 obj3 = new Object(obj1, obj2);
Object4 obj4 = new Object(obj3);
return someRandomMethodHere(obj4);;
}
}
Child Class:
public class ChildClass extends ParentClass {
protected String firstName = "Miley";
protected String lastName = "Cyrus";
protected int age = 27;
protected String city = "Los Angeles";
public boolean methodIWantToReuse() {
// Update data in Parent class first
return super.methodIWantToReuse(this.firstName, this.lastName, this.age, this.city);
}
}
注意: 保持局部变量名称与 class 级别变量相同不是好的做法。但在这里保持不变只是为了理解。