初识抽象类

/*
* 抽象类:在提取父类的过程中,某一个共性的方法子类有各自不同的实现方式,那么该方法需要修饰为abstract,该类就是抽象类了
*/

abstract class 犬科{
public abstract void 吼叫();//没有实现的方法
}


class 狼 extends 犬科{
@Override
public void 吼叫() {
System.out.println("嗷嗷叫");
}
}

class 狗 extends 犬科{
@Override
public void 吼叫() {
System.out.println("汪汪叫");
}
}


=====================================================================================================================


//计算圆形和矩形的面积
abstract class Shape {
public abstract double getArea();
}

// 圆形面积
class Cricle extends Shape {
public final static double PI = 3.1415;
private double radius;

public Cricle(double radius) {
this.radius = radius;
}

@Override
public double getArea() {
return radius * radius * PI;
}
}


// 矩形形面积
class Rectangle extends Shape {
private double length;
private double width;

Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double getArea() {
return this.length * this.width;
}
}



public class Demo5 {
public static void main(String[] args) {
Cricle cricle = new Cricle(100);
System.out.println(cricle.getArea());

Rectangle r = new Rectangle(100,100);
System.out.println(r.getArea());
}
}

/*
* 继承了父类中的抽象方法,含有抽象方法,如果没有重写给抽象方法,那么该类也是抽象类
*/

=====================================================小例子-=======================================

/*抽象类:是因为子类对父类有不同的实现方法
*
* 1.抽象类一定是父类吗?
* 答:抽象类一定是父类
*
* 2.抽象类有构造方法吗?
* 答:有构造方法
*
* 抽象类和普通类的区别?
* 相同点:都是类,都是在描述事物
* 不同点:1.抽象类不能创建对象,普通类可以
* 2.抽象类可以含有抽象方法,普通类不能
*
* 3.abstract不能喝哪些关键字同时使用?
* 答:1:final:final修饰的方法不能被重写,abstract修饰的方法必须能重写
* 2:static:static修饰的方法可以直接通过类名直接调用,abstract修饰的方法不能通过类名调用
* 3:private:private修饰的方法不可以被重写,abstract修饰的方法必须能被重写
*
* 4.抽象类一定含有抽象方法吗?
* 不一定,当不希望这个类创建对象时,可以把这个类修饰为抽象类
*
*/

/*
* 需求:公司中程序猿有姓名,工号,薪水,工作内容
* 项目经理除了有姓名,工号,薪水,还有奖金,工作内容
* 对给出需求进行数据建模
*/

abstract class Employee {
private String name;
private String id;
private double money;

public String getName() {
return name;
}

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

public String getId() {
return id;
}

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

public double getMoney() {
return money;
}

public void setMoney(double money) {
this.money = money;
}

public Employee(String name, String id, double money) {
this.name = name;
this.id = id;
this.money = money;
}

public Employee() {
super();
}

public abstract void work();

}

// 程序猿类
class Programmer extends Employee {

public Programmer() {
}

public Programmer(String name, String id, double money) {
super(name, id, money);
}

@Override
public void work() {
System.out.println(Programmer.this.getName() + "是一名程序猿,工号是"
+ Programmer.this.getId() + "月薪为" + Programmer.this.getMoney()
+ "职责是写代码");
}

}

// 项目经理类
class Manager extends Employee {
// 一个私有属性
private double bonus;

public double getBonus() {
return bonus;
}

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

public Manager(String name, String id, double money, double bonus) {
super(name, id, money);
this.bonus = bonus;
}

public Manager() {
}

@Override
public void work() {
System.out.println(Manager.this.getName() + "是一项目经理,工号是"
+ Manager.this.getId() + "月薪为" + Manager.this.getMoney()
+ "奖金为" + Manager.this.getBonus()+"职责为管理项目");
}

}

public class Demo5 {
public static void main(String[] args) {
Programmer programmer = new Programmer("张三", "1001", 15000);
Manager manager = new Manager("小孟", "1000", 25000, 1000);

programmer.work();
manager.work();
}
}