Cách sử dụng từ khóa super trong java
import java.util.*;
class person{
private int id;
public String name;
person(){
this.id = 0;
this.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:");
id=objSc.nextInt();
objSc.nextLine();
System.out.print("Nhap name:");
name=objSc.nextLine();
}
public void output() {
System.out.println("Id:"+this.id+" "+"Name:"+this.name);
}
}
class employee extends person{
public int empId;
public float salary;
employee(){
super();
this.empId =0;
this.salary=0;
}
public void input() {
super.input();
Scanner objSc =new Scanner(System.in);
System.out.print("Nhap empId:");
this.empId=objSc.nextInt();
objSc.nextLine();
System.out.print("Nhap salary:");
this.salary = objSc.nextFloat();
}
public void output() {
super.output();
System.out.println("empId"+this.empId+"Salary:"+this.salary);
}
}
public class demoInheritance {
public static void main(String[] args) {
employee emp = new employee();
emp.input();
emp.output();
}
}
Trương Đình Huy
» Tin mới nhất:
» Các tin khác: