面向对象---抽象类

一、抽象类的概念

Java中,如果一个类中有一个方法声明(抽象方法)抽象功能,那么这个类定义为抽象类

关键字:abstract  

抽象类的特点:抽象类不能直接实例化!(不能创建对象) 接口也不能实例化

  关于抽象类:

1)如果一个类中有抽象方法,那么这个类是一定是一个抽象类

2)抽象类中不一定都是抽象方法

   抽象类的子类

1)如果子类是抽象类,没有意义,因为都不能实例化,对象如何创建

2)子类具体类,那么子类必须实现父类中的抽象功能.

具体代码:

//抽象的动物类
abstract class Animal{
	
	//提供一个抽象方法(只是一个声明)
	public abstract void eat() ;  //抽象功能,没有方法体,需要子类实现抽象类的这个功能
	
	//非抽象功能
	public void method() {
		
	}
}
//具体的动物类
class Cat extends Animal{
	//实现抽象类的方法
	@Override
	public void eat() {
		System.out.println("猫吃鱼....");
	}
}
class Dog extends Animal{

	@Override
	public void eat() {
		System.out.println("狗吃骨头....");
	}	
} 
//测试类
public class AbstractDemo {
	public static void main(String[] args) {
		Animal a = new Cat() ;
		Animal a2 = new Dog() ;
	}
}

二、抽象类的成员特点

抽象类的成员特点:
   成员变量:可以是变量,也是一个常量
  构造方法:可以有无参,可以有有参,作用:给对象进行初始化的.
  成员方法:可以有抽象方法,还可以有非抽象方法...

  abstract和哪些关键字是冲突的,不能共有!
  abstract 和private
abstract 和final

abstract和static 

举个例子体现一下:

//抽象的人类
abstract class Person{
	
//	private abstract void function() ;
//	public final abstract void function() ; .//错误的,非法的 
//	public static abstract void function() ;//非法的
	
	private String name ;
	int num = 10 ; //变量
	public final int num2 = 100 ;//常量
	
	//无参构造
	public Person() {
		
	}
	
	//有参构造
	public Person(String name) {
		this.name = name ;
	}
	
	//非抽象的功能
	public void show() {
		System.out.println(num);
		System.out.println(num2);
	}
	
	//抽象功能
	public abstract void method();
}
//学生类
class Student extends Person{

	@Override
	public void method() {
		System.out.println("method student...");
	}
	
}
//测试类
public class AbstractDemo2 {
	
	public static void main(String[] args) {
		
		//创建对象
		Person p = new Student();  //=号左边:抽象类  =号右边:具体类		形式:抽象类多态..
		p.show();
		p.method();
	}
}

大家可以试着用抽象类来练习一下猫狗案例:

提示:Animal类定义为抽象类,Cat和Dog类定义为具体的实现类,且继承Animal类

-------------------------------------华丽分割线--------------------------------------

具体代码:

/**
 * 抽象的动物类
 * @author Administrator
 *
 */
public abstract class Animal {

	private String name ;
	private int age ;
	
	public Animal() {
		super();
	}

	public Animal(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	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 abstract void eat() ;
	
}
//具体猫类
public class Cat extends Animal {
	
	

	public Cat() {
		super();
	}

	public Cat(String name, int age) {
		super(name, age);
	}

	@Override
	public void eat() {
		System.out.println("猫吃鱼...");
	}
	
	//自己的功能
	public void playGame() {
		System.out.println("会玩毛线...");
	}

}

//狗类
public class Dog extends Animal {
	
	

	public Dog() {
		super();
	}

	public Dog(String name, int age) {
		super(name, age);
	}

	@Override
	public void eat() {
		System.out.println("狗吃骨头...");
	}
	
	public void lookDoor() {
		System.out.println("会看门...");
	}

}

//测试类
public class AnimalDemo {

	public static void main(String[] args) {
		
		//测试猫类
		//方式1:无参/set
		Animal a = new Cat() ; //抽象类多态
		a.setName("TOM");
		a.setAge(5);
		System.out.println("猫的姓名是:"+a.getName()+",年龄是:"+a.getAge());
		a.eat();
//		a.playGame() ;
		
		//要么向下转型,要么具体类创建具体类   
		Cat c = (Cat)a;
		c.playGame();
		
		
		System.out.println("------------------------");
		
		//方式2:带参构造创建对象 (具体类创建具体类对象)
		Cat c2 = new Cat("TOM", 3) ;
		System.out.println("猫的姓名是:"+c2.getName()+",年龄是:"+c2.getAge());
		c2.playGame();
		c2.eat();
		
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值