📄 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);// 给员工与学生对象数组赋值
people[1]= new Student("李四", "计算机科学");
for (int i = 0; i < people.length; i++)// 打印输出关于人类的基本信息
{
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);// 传递信息到父类构造函数
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 :学生
{
public Student(String n, String m)
{
super(n);// 传递字符n到父类构造函数
major = m;
}
public String getDescription()//子类2 的方法 ,得到学生的专业
{
return "学生的专业是:" + major;
}
private String major;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -