毕业了重新学java_毕业两年半,重新学JAVA(二)继承

1.继承

类Employee:

public class Employee {

// 姓名

private String name;

// 年龄

private int age;

// 薪水

private int salary;

// 构造函数

public Employee(String name, int age, int salary) {

// TODO 初始化

this.name = name;

this.age = age;

this.salary = salary;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public int getSalary() {

return salary;

}

public void setSalary(int salary) {

this.salary = salary;

}

}类Manager继承类

Employee

public class Manager extends Employee {

/**

* 构造函数

*

* @param name

* @param age

* @param salary

* @param bonus

* @param position

*/

public Manager(String name, int age, int salary, int bonus, String position) {

// 调用父类构造函数的写法

super(name, age, salary);

// TODO 当前类变量初始化

this.bonus = bonus;

this.position = position;

}

// 奖金

private int bonus;

// 职位

private String position;

public int getBonus() {

return bonus;

}

public void setBonus(int bonus) {

this.bonus = bonus;

}

public String getPosition() {

return position;

}

public void setPosition(String position) {

this.position = position;

}

@Override

public int getSalary() {

// TODO 重写父类中的getSalary()方法,返回经理的薪水=薪水+奖金

int total = super.getSalary() + bonus;

return total;

}

}类

Employee 被称谓父类或超类,Manager类为子类。

Manager类继承了Employee类中的方法

package com.text;

/**

* 继承

* @author Dell

*

*/

public class MainClass {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

// 实例化Employee对象

Employee employee = new Employee("summer", 10, 3456);

employee.getName();

employee.getAge();

employee.getSalary();

// 实例化Manager对象

Manager manager = new Manager("summer", 10, 3456, 1000, "经理");

// 继承了Employee中的方法

manager.getName();

manager.getAge();

manager.getSalary();

// 本类中的方法

manager.getBonus();

manager.getPosition();

}

}

此时,当Manager类使用getSalary()方法获取经理的薪水时,其实是错误的,因为经理的薪水需要加上bonus(奖金)

这样就需要在Manager类中重写Employee类中的getSalary()方法,如下:

@Override

public int getSalary() {

// TODO 重写父类中的getSalary()方法,返回经理的薪水=薪水+奖金

int total = super.getSalary() + bonus;

return total;

}

输出Manager的工资System.out.println(manager.getSalary());

结果为:4456

@Override代表重写

此处引出了super关键字,

super.getSalary()代表引用父类Employee中的getSalary()方法,而不是本类中的getSalary().

Manager类的构造函数中->super(name, age, salary);代表调用其父类Employee中的构造函数.

一个父类可以有多个子类.java不支持多继承.

0818b9ca8b590ca3270a3433284dd417.png

2.多态

3.抽象类

1.有抽象方法的类,一定是抽象类,抽象类中的方法,不一定是抽象方法.

2.抽象类的子类如果不定义为抽象类,则需要实现父类的所有抽象方法,否则,则需将子类定义为抽象类.

package com.abstractclass;

/**

* 抽象类

* @author Dell

*

*/

public abstract class BaseClass {

public abstract String getDescription();

public BaseClass(String name) {

// TODO Auto-generated constructor stub

this.name = name;

}

private String name;

public String getName() {

return name;

}

}

package com.abstractclass;

public class ChildClass extends BaseClass {

private String major;

public ChildClass(String name, String major) {

super(name);

this.major = major;

// TODO Auto-generated constructor stub

}

public String getMajor() {

return major;

}

@Override

public String getDescription() {

// TODO Auto-generated method stub

return null;

}

}

4.访问修饰符

1.private->仅对本类可见

2.public->对所有类可见

3.protected->对本包和所有子类可见

4.默认(不添加修饰符)->对本包可见(不推荐)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值