Thừa kế và đa hình trong Java
package testDemo;
import java.util.*;
interface IELECTRONIC_DEVICE {
double sum_total();
}
abstract class TV implements IELECTRONIC_DEVICE {
private String tvID;
private String manifacturer;
private String entryDate;
private double price;
private double num;
public TV(String tvID, String manifacturer, String entryDate, double price, double num) {
this.tvID = tvID;
this.manifacturer = manifacturer;
this.entryDate = entryDate;
this.price = price;
this.num = num;
}
public TV() {
this.tvID = "";
this.manifacturer = "";
this.price = 0;
this.entryDate = "";
this.num = 0;
}
public String getTvID() {
return tvID;
}
public void setTvID(String tvID) {
this.tvID = tvID;
}
public String getManifacturer() {
return manifacturer;
}
public void setManifacturer(String manifacturer) {
this.manifacturer = manifacturer;
}
public String getEntryDate() {
return entryDate;
}
public void setEntryDate(String entryDate) {
this.entryDate = entryDate;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getNum() {
return num;
}
public void setNum(double num) {
this.num = num;
}
public double sum_total() {
return 0;
}
void input() {
Scanner objSc = new Scanner(System.in);
System.out.print("tvID:");
this.setTvID(objSc.nextLine());
System.out.print("manifacturer:");
this.setManifacturer(objSc.nextLine());
System.out.print("entryDay:");
this.setEntryDate(objSc.nextLine());
System.out.print("Don gia:");
this.setPrice(objSc.nextDouble());
System.out.print("So luong:");
this.setNum(objSc.nextDouble());
}
void output() {
System.out.println("tvID:" + this.getTvID());
System.out.println("entryDate:" + this.getEntryDate());
System.out.println("manifacturer:" + this.getManifacturer());
System.out.println("Don gia:" + this.getPrice());
System.out.println("So luong:" + this.getNum());
}
abstract double discount();
}
class TV_SAMSUNG extends TV {
String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
double discount() {
double dis = 0;
if (this.getState().equals("new")) {
dis = this.getNum() * this.getPrice() * 0.1;
} else {
dis = this.getNum() * this.getPrice() * 0.6;
}
return dis;
}
public double sum_total() {
double sum = 0;
sum = this.getNum() * this.getPrice() - this.discount();
return sum;
}
void input() {
super.input();
Scanner objSc = new Scanner(System.in);
System.out.print("State:");
this.setState(objSc.nextLine());
}
void output() {
super.output();
System.out.println("state:" + this.getState());
}
}
public class demo {
public static void main(String[] args) {
}
}
Trương Đình Huy