Java 如何在抽象类中使用 getter 和 setter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22649908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use getters and setters in an Abstract Class
提问by Eric
I have an abstract class Customer. It's a very simple class, only setting 5 stringvariables as well as 5 static intvariables. Better to show what I mean by this:
我有一个抽象类Customer。这是一个非常简单的类,只设置了5个string变量和5个static int变量。更好地说明我的意思:
As a disclaimer I made the code as simple as possible, I have more logic involved in my abstract class that doesn't pertain to the question.
作为免责声明,我使代码尽可能简单,我的抽象类中涉及更多与问题无关的逻辑。
Abstract Class
抽象类
public abstract class Customer {
private String Name, Address, Phone, Email, Company;
public static final int NAME = 0, ADDRESS = 1, PHONE = 2, EMAIL = 3, COMPANY = 4;
public Customer(String Name, String Address, String Phone, String Email, String Company) {
setValues(Name, Address, Phone, Email, Company);
}
private void setValues(String Name, String Address, String Phone, String Email, String Company) {
setName(Name);
setAddress(Address);
setPhone(Phone);
setEmail(Email);
setCompany(Company);
}
//declare public getters and setters methods below
}
My question is as follows:
我的问题如下:
I have a classthat extendsthis abstract classcalled Customer(different package). If I set up the constructorin this classas such:
我有一个class是extends这个abstract class所谓的Customer(不同的包)。如果我成立了constructor这个class如此:
Object Class
项目等级
public class Customer extends Main.Customer {
private String Name, Address, Phone, Email, Company;
public Customer(String Name, String Address, String Phone, String Email, String Company) {
super(Name, Address, Phone, Email, Company);
}
}
Does this set my Stringvariables as to whatever I pass through the constructor? As in when I instantiatethis classas an object, how would I be able to 'get' a variable from it?
For example: (Assume String1 -String5are stringsof some sort)
这是否将我的String变量设置为我通过构造函数传递的任何内容?就像当我把instantiate它class作为一个object,我怎么能从中“得到”一个变量?
例如:(假设String1 -String5是strings某种类型)
public class Random {
private Customer customer = new Customer(String1, String2, String3, String4, String5);
}
How would I then call the object later on in the class to return a string(of any single variable). As in if my abstract classwasn't abstractbut the main classI was using to instantiateas an object, I'd get the variable like so: String name = customer.getName();
稍后我将如何在类中调用该对象以返回string(任何单个变量的)。就像我abstract class不是,abstract而是class我使用的主要instantiate作为object,我会得到这样的变量: String name = customer.getName();
TL;DR:
特尔;博士:
Just unsure how to get variablesfrom an objectextendingan abstract class.
只是不知道该如何获得variables从objectextending一个abstract class。
采纳答案by lreeder
Drop the variables from your subclass so they don't shadow the variables with the same name in the parent class.
从您的子类中删除变量,这样它们就不会影响父类中具有相同名称的变量。
//sub class
public class Customer extends Main.Customer {
//DROP THESE private String Name, Address, Phone, Email, Company;
public Customer(String Name, String Address, String Phone, String Email, String Company) {
super(Name, Address, Phone, Email, Company);
}
}
And add getters to your parent class:
并将 getter 添加到您的父类:
//parent class
public abstract class Customer {
private String Name, Address, Phone, Email, Company;
public static final int NAME = 0, ADDRESS = 1, PHONE = 2, EMAIL = 3, COMPANY = 4;
public Customer(String Name, String Address, String Phone, String Email, String Company) {
setValues(Name, Address, Phone, Email, Company);
}
private void setValues(String Name, String Address, String Phone, String Email, String Company) {
setName(Name);
setAddress(Address);
setPhone(Phone);
setEmail(Email);
setCompany(Company);
}
public String getName() {
return Name;
}
public String getAddress() {
return Address;
}
//etc....
}
Also, I really recommend using different names for your parent and subclass to avoid confusion.
另外,我真的建议您为父类和子类使用不同的名称以避免混淆。
回答by Kavka
Your subclass is overshadowing the private properties of the abstract class.
您的子类掩盖了抽象类的私有属性。
public abstract class Customer {
private String Name, Address, Phone, Email, Company;
public class Customer extends Main.Customer {
private String Name, Address, Phone, Email, Company;
so any get methods in your abstract class of the form
所以你的表单抽象类中的任何 get 方法
public String getName() {
return Name;
}
would return the never initialized variable name in the subclass. Whereas, when you call super(...), the set functions there would set the variables of the abstract class.
将返回子类中从未初始化的变量名。而当您调用 时super(...),那里的 set 函数将设置抽象类的变量。
So you are setting one set of variables, but reading another set of variables that were never initialized.
因此,您正在设置一组变量,但读取从未初始化的另一组变量。
回答by nachokk
Some considerations before start:
开始前的一些注意事项:
- In java by code-convention variables starts with lower-case. It will help code readability for people including you.
Don't have two classes with the same name, is very confusing. You can call it for example
ACustomerorAbstractCustomerand the other oneCustomerorSomethingCustomerIt isn't
Object classit'sConcrete Classa class that you can have instances of it.As Customer inherits ACustomer you don't have to define again the
ACustomerfields, Customer already has them. If you do you are hidingthose from parent.
- 在java中,代码约定变量以小写开头。这将有助于包括您在内的人的代码可读性。
不要有两个同名的类,很混乱。例如,您可以将其称为
ACustomerorAbstractCustomer和另一个Customer或SomethingCustomer这不是
Object class它的Concrete Class一类,你可以有它的实例。由于 Customer 继承了 ACustomer,您不必再次定义这些
ACustomer字段,Customer 已经拥有它们。如果你这样做,你就是在向父母隐藏这些。
public class Customer extends ACustomer {
public Customer(String name, String address, String phone, String email, String company) {
super(name, address, phone, email, company);
}
}
- You are calling an overrideable method inside the constructor take care about that, cause if
setXXXis override then perhaps you could have aNullPointerException. - For your question in how to get member you can define
getters.
- 您正在构造函数中调用一个可覆盖的方法,请注意这一点,因为如果
setXXX被覆盖,那么也许您可以有一个NullPointerException. - 对于如何获得会员的问题,您可以定义
getters.
public abstract class ACostumer{
private String name;
public String getName() {
return name;
}
}
Then in client code:
然后在客户端代码中:
ACustomer customer = new Customer(...);
customer.getName();

