欢迎光临
我们一直在努力

面向对象的继承,封装

public class Demo {
public static void main(String[] args) {
ProjectManager projectManager1=new ProjectManager("男",3341,"张三",15000,80,1500);
ProjectManager projectManager2=new ProjectManager("女",1308,"小美",12000,70,3000);
Programer programer1=new Programer("男",2312,"唐三",20000,"热门");
Programer programer2=new Programer("女",2332,"小黄",21000,"热门");
Test test=new Test();
test.show(projectManager1);
test.show(projectManager2);
test.show( programer1 );
test.show( programer2);
CalculateSalary calculateSalary=new CalculateSalary();
calculateSalary.salary(programer1);
calculateSalary.salary(projectManager1);

}
}

class Employee{
private int id;
private String name;
private String gender;
private int salary;

public String getGender() {
return gender;
}

public int getId() {
return id;
}

public String getName() {
return name;
}

public int getSalary() {
return salary;
}

public void setGender(String gender) {
this.gender = gender;
}

public void setId(int id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public void setSalary(int salary) {
this.salary = salary;
}

public Employee(String gender, int id, String name, int salary) {
this.gender = gender;
this.id = id;
this.name = name;
this.salary = salary;
}
public Employee(){

}
}
class ProjectManager extends Employee{
private int exp;
private int bonus;
public ProjectManager(String gender, int id, String name, int salary,int exp,int bonus){
super(gender, id, name, salary);
this.exp=exp;
this.bonus=bonus;
}
public int getBonus() {
return bonus;
}

public int getExp() {
return exp;
}

public void setBonus(int bonus) {
this.bonus = bonus;
}

public void setExp(int exp) {
this.exp = exp;
}

public String show(){
return null;
}
}
class Programer extends Employee{
private String hot;
//public Programer(){ }

public Programer(String gender, int id, String name, int salary, String hot) {
super(gender, id, name, salary);
this.hot = hot;
}

public String getHot() {
return hot;
}

public void setHot(String hot) {
this.hot = hot;
}

public String show(){
return null;
}
}
class Test{
public void show(ProjectManager projectManager){
System.out.println(" 性别: "+projectManager.getName()+
" id: "+projectManager.getId()+ " 名字: "+
projectManager.getName()+ " 薪水: "+projectManager.getSalary() +
" 经验: " +projectManager.getExp() + " 分红: "
+ projectManager.getBonus());
}
public void show(Programer p){
System.out.println("程序员:" + p.getName() + " 工号:" + p.getId()
+ " 性别:" + p.getGender() + " 薪资:" + p.getSalary()
+ " 热门:" + p.getHot());
}
}
class CalculateSalary{
public void salary(ProjectManager p1){
System.out.println("程序员"+p1.getName()+"的工资为"+(p1.getSalary()+p1.getBonus())+"元");
}
public void salary(Programer p2)
{
System.out.println("项目经理"+p2.getName()+"的工资为"+p2.getSalary()+"元");
}
}

public class Person {

private int id;
private String name;
private int age;

public Person(int age, int id, String name) {
this.age = age;
this.id = id;
this.name = name;
}

public int getAge() {
return age;
}

public int getId() {
return id;
}

public String getName() {
return name;
}

public void setAge(int age) {
this.age = age;
}

public void setId(int id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public void max(Person other){
if (this.age>other.age){
System.out.println(this.getName()+"的年龄大于"+other.getName()+"的年龄");
} else if (this.age==other.age) {
System.out.println(this.getName()+"的年龄等于"+other.getName()+"的年龄");
}else{
System.out.println(this.getName()+"的年龄小于"+other.getName()+"的年龄");
}
}

}
class A{
public static void main(String[] args) {
Person p1=new Person(18,1234,"张三");
Person p2=new Person(20,1232,"李四");
p1.max(p2);
}
}

public class Demo1 {
public static void main(String[] args) {
Animal animal=new Bird();
animal.move();

}
}

class Animal{
String name;
int age;
public void move(){
System.out.println("动物有移动的行为");
}
}
class Bird extends Animal{
String color;

@Override
public void move() {
super.move();
System.out.println("奥迪会飞");
}
}只有一句话,多练多写才会懂

赞(0)
未经允许不得转载:171主机测评 » 面向对象的继承,封装
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址