부모 클래스가 Product
이고 자식이 Food
입니다. 모든 Product
에는 배달 시간이 있습니다. 예를 들어 Food
의 경우 1 일입니다 (하나의 int
으로 정의 됨). 정적 값을 사용한 상속
Product
클래스를 만든 :
public abstract class Product {
private int id;
private String description;
private double price;
public Product(int id, String description, double price) {
this.id = id;
this.description = description;
this.price = price;
}.... and so on
내 Food
클래스는 다음과 같습니다
public class Food extends Product{
private Date expirationDate;
private static final int DELIVERY = 1;
public Food(int id, String description, double price, Date expirationDate) {
super(id, description, price);
this.expirationDate = expirationDate;
}.. and so on
이이 일을 적절한 방법이 있나요? 둘째, 내 변수 DELIVERY
을 Food
에서 어떻게 호출 할 수 있습니까?
희망 사항 저는 제 질문에 분명합니다.
"이 작업을 수행하는 적절한 방법입니까?" - 뭐라구? 계승? 훌륭해. "어떻게 변수를 '식품'에서 '전달'이라고 부를 수 있습니까? 그냥 사용하십시오. 정의되어 있으므로 아무런 문제가 없습니다. – AlexR
@AlexR : 부모로부터 자식 필드에 어떻게 액세스 할 수 있습니까? – mok
전달을 위해 부모 클래스에서 변수를 다른 방법으로 지정해야합니까? – ddvink