Fork me on GitHub

Java-12 抽象类

抽象类

  • 抽象类:用abstract修饰的类
  • 书写方法:
abstract 修饰符 返回值类 方法名(参数列表)
  • 注意事项:
1.抽象类中可以没有抽象方法,有抽象方法的类一定是抽象类
2.抽象类本身不能创建对象,只能依靠子类向上转型方式创建
	抽象类的子类:继承了抽象类的类,抽象类的子类要么实现抽象类中所有的抽象方法,要么自己本身是一个抽象类,实现抽象方法:在子类中重写父类中的抽象方法,给出具体方法体。
3.抽象类不能使用final修饰。抽象方法也不能使用static修饰
4.抽象类中有构造方法的。完成数据初始化,为了让子类调用
  • 示例:
package chouxiang;

public class AbstractDemo1 {
	public static void main(String[] args) {
		Animal a = new Cat(); // 向上转型,也叫抽象类的多态
		a.sleep();
	}
}

abstract class Animal{
	abstract public void sleep();
}


class Cat extends Animal{
	// 重写sleep方法
	public void sleep() {
		System.out.println("睡觉了啊!");
	}
}
  • 抽象类练习1
package chouxiang;

public class AbstractDemo2 {
	public static void main(String[] args) {
		// 使用抽象类的多态,调用方法
		A a = new B();
		a.a();
		a.b();
	}
}

abstract class A{
	// 定义2个抽象类
	abstract public void a();
	abstract public void b();
}

class B extends A{
	// 子类实现2个抽象类方法
	public void a() {
		System.out.println("打印A");
	}
	public void b() {
		System.out.println("打印B");
	}
}
  • 抽象类练习2
package chouxiang;

public class AbstractDemo3 {
	public static void main(String[] args) {
		Shape r = new Rect(20, 10);
		Shape c = new Circle(5);
		print(r);
		print(c);
	}
	public static void print(Shape s) {
		System.out.println("周长为:" + s.Primeter());
		System.out.println("面积为:" + s.area());
	}
}

abstract class Shape{
	abstract public double Primeter();
	abstract public double area();
} 

class Rect extends Shape{
	double length;
	double width;
	public Rect() {
		
	}
	public Rect(double length, double width) {
		this.length = length;
		this. width =  width;
	}
	@Override
	public double Primeter() {
		return 2*(length + width);
	}

	@Override
	public double area() {
		return length*width;
	}
}

class Circle extends Shape{
	double r;
	public static final double PI = 3.14;
	public Circle() {
		
	}
	public Circle(double r) {
		this.r = r;
	}
	@Override
	public double Primeter() {
		return 2 * PI  * r;
	}
	@Override
	public double area() {
		return PI  * r * r;
	}
}
  • 继承关系和组合关系示例
package chouxiang;

public class AbstractDemo4 {
	public static void main(String[] args) {
		Person p = new Person();
		p.setName("小明");
		p.setAge(18);
		p.setGender('男');
		Phone m = new Phone();
		m.setBrand("小米");
		m.setColor("绿色");
		m.setPrice(1999);
		m.setType("米8");
		p.setPhone(m);
		
		Person p2 = new Person("小王",22,'女',new Phone("苹果", "11","绿色", 4999));
		p.show();
		p2.show();
		
	}
}


class Phone{
	private String brand;
	private String type;
	private String color;
	private double price;
	// 无参构造方法
	public Phone() {
		super();
	}
	// 有参构造方法
	public Phone(String brand, String type, String color, double price) {
		super();
		this.brand = brand;
		this.type = type;
		this.color = color;
		this.price = price;
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	
}
// Person 和 Phone关系为组合关系
class Person{
	private String name;
	private int age;
	private char gender;
	private Phone phone;
	public Person() {
		super();
	}
	public Person(String name, int age, char gender, Phone phone) {
		super();
		this.name = name;
		this.age = age;
		this.gender = gender;
		this.phone = phone;
	}
	public void show() {
		System.out.println("姓名:" + name + ",年龄:" + age + ", 性别:" + gender + ",手机信息:" + phone.getBrand()+ "_" + phone.getColor()+ "_" + phone.getPrice() + "_"+ phone.getType());
	}
	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 char getGender() {
		return gender;
	}
	public void setGender(char gender) {
		this.gender = gender;
	}
	public Phone getPhone() {
		return phone;
	}
	public void setPhone(Phone phone) {
		this.phone = phone;
	}
	
}
posted @ 2020-10-04 10:29  是阿凯啊  阅读(180)  评论(0编辑  收藏  举报