?? persontest.java
字號:
//PersonTest.java
import java.text.*;
public class PersonTest
{
public static void main(String[] args)
{
Person[] people = new Person[2];
people[0]= new Employee("張三", 50000);// 給員工與學(xué)生對象數(shù)組賦值
people[1]= new Student("李四", "計(jì)算機(jī)科學(xué)");
for (int i = 0; i < people.length; i++)// 打印輸出關(guān)于人類的基本信息
{
Person p = people[i];
System.out.println(p.getName() + ", " + p.getDescription());
}
}
}
abstract class Person//抽象人類
{
public Person(String n)
{
name = n;
}
public abstract String getDescription();//抽象方法
public String getName()
{
return name;
}
private String name;
}
class Employee extends Person//子類1:員工
{
public Employee(String n, double s)
{
super(n);// 傳遞信息到父類構(gòu)造函數(shù)
salary = s;
}
public double getSalary()
{
return salary;
}
public String getDescription()//子類1的方法:員工工資
{
NumberFormat formatter
= NumberFormat.getCurrencyInstance();
return "員工的工資是:"+ formatter.format(salary);
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent/100;
salary += raise;
}
private double salary;
}
class Student extends Person//子類2 :學(xué)生
{
public Student(String n, String m)
{
super(n);// 傳遞字符n到父類構(gòu)造函數(shù)
major = m;
}
public String getDescription()//子類2 的方法 ,得到學(xué)生的專業(yè)
{
return "學(xué)生的專業(yè)是:" + major;
}
private String major;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -