-2
我試圖將AirlineTicket傳遞到類客戶中的方法addPoints。我想Customer.addPoints()方法能夠訪問AirlineTicket.getPrice()將類作爲參數傳遞到另一個類方法
Customer.java
public class Customer {
private String name;
private String address;
private int membershipNumber;
private int membershipPoints;
public void applyPoints(Class AirlineTicket){
int ticketPrice = AirlineTicket.getPrice();
}
//zero-argument constructor
public Customer() {
super();
}
//3-argument constructor with points set to 0
public Customer(String name, String address, int membershipNumber) {
this.name = name;
this.address = address;
this.membershipNumber = membershipNumber;
membershipPoints = 0;
}
//to-string
@Override
public String toString() {
return "Customer [name=" + name + ", address=" + address + ", membershipNumber=" + membershipNumber
+ ", membershipPoints=" + membershipPoints + "]";
}
//public getters protected setters
public String getName() {
return name;
}
protected void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
protected void setAddress(String address) {
this.address = address;
}
public int getMembershipNumber() {
return membershipNumber;
}
protected void setMembershipNumber(int membershipNumber) {
this.membershipNumber = membershipNumber;
}
public int getMembershipPoints() {
return membershipPoints;
}
protected void setMembershipPoints(int membershipPoints) {
this.membershipPoints = membershipPoints;
}
}
AirlineTicket.java的價值
public class AirlineTicket {
private Customer passenger;
private Flight flight;
private double price;
//zero-argument constructor
public AirlineTicket() {
super();
}
//3-argument constructor
public AirlineTicket(Customer passenger, Flight flight, double price) {
super();
this.passenger = passenger;
this.flight = flight;
this.price = price;
}
//to-string
@Override
public String toString() {
return "AirlineTicket [passenger=" + passenger + ", flight=" + flight + ", price=" + price + "]";
}
//public getters and protected setters
public Customer getPassenger() {
return passenger;
}
protected void setPassenger(Customer passenger) {
this.passenger = passenger;
}
public Flight getFlight() {
return flight;
}
protected void setFlight(Flight flight) {
this.flight = flight;
}
public double getPrice() {
return price;
}
protected void setPrice(double price) {
this.price = price;
}
}
你實際上是試圖通過一個Class類型的變量?或者是一個ClassA類型的變量? –
遵循Java命名約定並使用有意義的描述性名稱,您會發現代碼變得更容易理解。目前,您有一個名爲'ClassA'類的'ClassA'的變量。你沒有向我們展示一個名爲'Class'的類,所以我們不知道它有什麼getters/setters。但是命名與另一個類同名的變量不會使其成爲另一個類的實例。 – David
非常好。現在究竟是什麼問題? –