Cách cài đặt khái niệm thừa kế bằng ngôn ngữ lập trình Java
import java.util.*;
class person {
int id;
String name;
person(int id, String name) {
this.id = id;
this.name = name;
}
public void input() {
Scanner objSc = new Scanner(System.in);
System.out.print("Nhap id:");
this.id = objSc.nextInt();
objSc.nextLine();
System.out.print("Nhap ten:");
this.name = objSc.nextLine();
}
public void output() {
System.out.println("id " + this.id + "name " + this.name);
}
}
class employee extends person {
int empId;
float salary;
employee(int id, String name, int empId, float salary) {
super(id, name);
this.empId = empId;
this.salary = salary;
}
public void input() {
Scanner objSc = new Scanner(System.in);
super.input();
System.out.print("empID:");
this.empId = objSc.nextInt();
System.out.print("Salary:");
this.salary = objSc.nextFloat();
}
public void output() {
super.output();
System.out.println("empId " + this.empId + "salary " + this.empId);
}
}
public class demo {
public static void main(String[] args) {
employee emp = new employee(10,"huytd",10,100);
emp.input();
emp.output();
}
}
Trương Đình Huy
» Tin mới nhất:
» Các tin khác: