-2
私はAirlineTicketをClass CustomerのメソッドaddPointsに渡そうとしています。 Iは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;
}
}
を行うことができます:私はあなたがこのような何かをしたいと思いますか?またはClassA型の変数? –
Javaの命名規則に従っていて、わかりやすい記述名を使用すると、コードが理解しやすくなります。現在、 'Class'型の' ClassA'という変数があります。あなたは私たちにゲッター/セッターが何であるか分からないように、 'クラス 'というクラスを私たちに示していません。しかし、変数を別のクラスと同じ名前で命名しても、他のクラスのインスタンスにはなりません。 – David
とてもいいです。今ここで問題は正確に何ですか? –