我试图将AirlineTicket传递给Class Customer中的方法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;
}
}
答案 0 :(得分:1)
public void applyPoints(Class AirlineTicket){
一个问题是,这不是AirlineTicket
类型的变量。您有一个Class
类型的变量。我想你会想要这样的东西:
public void applyPoints(AirlineTicket ticket){
然后你可以做
int ticketPrice = ticket.getPrice();