如何解决如何在子类中调用超类的参数,以便可以使用变量进行更多计算?
这是代码,当您对我要完成的事情感到好奇时仅供您参考:
import java.util.EmptyStackException;
public class TicketMain {
public static void main(String[] args) {
// Create a Student Ticket
StudentTicket t1 = new StudentTicket(100,true);
t1.setPromotionCode("KEXP call-in winner");
System.out.println(t1);
// Generate a general ticket
Ticket t2 = new Ticket(55,40);
System.out.println(t2);
// Generate a student ticket
StudentTicket t3 = new StudentTicket(90,false);
t3.setPromotionCode("KEXP call-in winner");
System.out.println(t3);
// Check for equality
System.out.println("Ticket t1 and Ticket t2 are equal: " + t1.equals(t2)); // Should return false
System.out.println("Ticket t1 and Ticket t3 are equal: " + t1.equals(t3)); // Should return true
// Total tickets generated
System.out.println("Total Tickets generated so far: " + Ticket.getTicketCount());
}
}
class Ticket {
private double price;
private int daysEarly;
private String promotionCode;
private static final String emptyString = "";
private static int ticketID; // Generates ticket number
public Ticket (double price,int daysEarly) { // Constructs ticket with given price,# of days early,no promo,assigns ticket #
this.price = price;
this.daysEarly = daysEarly;
ticketID += 1;
if (ticketID < 0) {
throw new EmptyStackException();
}
}
public int getDaysEarly(){
return daysEarly;
}
public double getPrice() {
return price;
}
public String getPromotionCode(){
if (promotionCode == null) return emptyString;
if (promotionCode.equals("KEXP call-in winner")) promotionCode = ("KEXP call in winner (student)");
return promotionCode;
}
public String setPromotionCode(String code){
if (code == null) throw new Error();
this.promotionCode = code;
return promotionCode;
}
public String toString(){
return "Ticket ID: " + ticketID + ",Price: $" + getPrice() + ",Days Early: " + getDaysEarly() + ",Promotion Code: " + getPromotionCode() + ".";
}
public static int getTicketCount(){
return ticketID;
}
public boolean equals(Ticket one) {
if (price == one.price && promotionCode.equals(one.promotionCode)) return true;
else return false;
}
}
以下是我要对超级参数进行数学运算的区域... 我在sub的参数中放入了“ price / 2”来进行数学处理,但是我仍然需要比喻另一个“ if语句”
class StudentTicket extends Ticket {
private static final int daysEarly = 14;
public StudentTicket(double price,boolean honors) {
/*
Student tickets are always bought by campus ticket sales agency,two weeks in advance.
Students always get 50% off initial price.
Honor students get an addition $5 off after the 50%,down to a minimum of $0.
Student tickets have special promo codes.
*/
super(price/2,daysEarly);
// HOW DO I CALL UPON SUPER AFTER ALREADY HAVING DONE SO ???
// I STILL NEED TO SUBTRACT 5 BASED ON HONORS BOOLEAN OF SUBCLASS
// HOW DO I ADD AN IF STATEMENT INTO PARAM OF SUPER ???
// All you will do is make a call to super constructor and pass appropriate parameters
// So the idea is when I create a instance of StudentTicket and return price it should return me a discounted.
// That means you need to override the getPrice() method in superclass to update the ticket price
}
}
有人告诉我重写getPrice方法,但是我不知道有什么方法可以访问子参数
解决方法
您可以在票证类中创建setPrice()方法:
...
public int getDaysEarly(){
return daysEarly;
}
public void setPrice(double newPrice){
price = newPrice;
}
public double getPrice() {
return price;
}
...
然后在StudentTicket构造函数中执行此操作:
public StudentTicket(double price,int daysEarly,boolean honors) {
super(price/2,daysEarly);
if(honors == true)
setPrice(getPrice() - 5);
}
因为您的方法是 public 类型,并且StudentTicket是Ticket类的子类,所以它应该能够访问和利用受任何 public 或保护的来自其父级的方法。
- 或者您可以像wang haoyu所说的那样,声明价格受保护。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。