抽象
抽象类定义与规则:
1、包含一个抽象方法的类必须是抽象类。
2、抽象类与抽象方法必须用abstract关键字声明。
3、抽象方法只需声明不需实现。
4、抽象类必须被继承,如果子类不是抽象类必须覆写抽象类的全部抽象方法。
定义格式;
abstract class 抽象类名称{
属性;
访问权限 返回值类型 方法名(参数)
[return 返回值];
}
访问权限 abstract 返回值类型 方法名称(参数);
}
public class AbstractDemo01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu=new Student();
stu.print();
}
}
abstract class Person{
private String name="人类";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void print();
}
class Student extends Person{
public void print() {
System.out.println("抽象类的属性:"+super.getName());
}
}
//结果
//抽象类的属性:人类
抽象类中定义构造方法
public class AbstractDemo02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu=new Student("张三",16,"清华");
stu.print();
}
}
abstract class Person{
private String name;
private int age;
public Person(String name,int age){
this.setAge(age);
this.setName(name);
}
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 print();
}
class Student extends Person{
private String school;
public Student(String name,int age,String school) {
super(name,age);
this.setSchool(school);
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public void print() {
System.out.println("姓名:"+super.getName()+",年龄:"+super.getAge()+",学校:"+this.getSchool());
}
}
//结果
//姓名:张三,年龄:16,学校:清华
接口
定义:在Java中,接口表示一种规范/约束/要求实现者必须遵循该规范:用来约束使用者应该怎么做。
定义格式:
interface 接口类名称{
全局变量;
抽象方法;
}
接口的抽象方法必须被定义为public访问权限。没看见public修饰只是隐藏了,不代表没有。
1、常见接口实现方法——一个类可以实现多个接口
public class InterfaceDemo01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu=new Student();
stu.printSchool();
stu.printPerson();
}
}
interface School{
public final static String name="TSINGHUA";
abstract void printSchool();
}
interface Person{
public final static int avgAge=20;
abstract void printPerson();
}
class Student implements School,Person{
public void printSchool() {
System.out.println("学校的名字:"+name);
}
public void printPerson() {
System.out.println("学生平均年龄为:"+avgAge);
}
}
//结果
//学校的名字:TSINGHUA
//学生平均年龄为:20
2、继承抽象类实现接口——既要实现接口又要继承抽象类
public class InterfaceDemo02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu=new Student();
stu.personInfo();
stu.schoolInfo();
}
}
interface Person{
public final static int age=20;
public abstract void personInfo();
}
abstract class School{
public final static String name="清华";
public abstract void schoolInfo();
}
class Student extends School implements Person{
public void personInfo() {
System.out.println("学校学生平均年龄为:"+age);
}
public void schoolInfo() {
System.out.println("学校名字:"+name);
}
}
//学校学生平均年龄为:20
//学校名字:清华
3、抽象类实现接口——一个抽象类能实现一个或多个接口(抽象类可以包含普通方法)
public class InterfaceDemo03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu=new Student();
stu.printPerson();
stu.printSchool();
}
}
interface School{
public static final String name="清华";
public abstract void printSchool();
}
abstract class Person implements School{
abstract void printPerson();
}
class Student extends Person {
public void printPerson(){
System.out.println("人类您好!");
}
public void printSchool(){
System.out.println("学校的名字:"+name);
}
}
//人类您好!
//学校的名字:清华
4、接口的继承——一个接口继承多个接口
public class InterfaceDemo04 {
public static void main(String[] args) {
// TODO Auto-generated method stub
X x=new X();
x.printA();
x.printB();
x.printC();
}
}
interface A{
public static final String name="清华";
public abstract void printA();
}
interface B{
public abstract void printB();
}
interface C extends A,B{
public abstract void printC();
}
class X implements C{
public void printA() {
System.out.println(name+"您好!");
}
public void printB() {
System.out.println("B、您好!");
}
public void printC() {
System.out.println("C、您好!");
}
}
//结果:
//清华您好!
//B、您好!
//C、您好!
抽象类与接口的区别:
区别点 | 抽象类 | 接口 |
---|---|---|
定义 | 包含一个抽象方法的类 | 包含全局常量和抽象方法的集合 |
组成 | 构造方法、抽象方法、普通方法、常量、变量 | 抽象方法、全局常量 |
使用 | 子类继承抽象类(extends) | 子类实现接口(implements) |
特殊点或关系 | 抽象类可以包含或实现多个接口 | 接口可以包含多个抽象类但不能继承抽象类,但能继承多个接口 |
常见设计模式 | 模板设计 | 工厂设计、代理设计 |
对象 | 都通过对象的多态产生实例化对象 | |
局限 | 抽象类有单继承的局限 | 接口无此局限 |
实际 | 作为一个模板 | 作为一个标准或表示一种能力 |
选择 | 如果抽象类和接口都能使用,优先选择接口(避免单继承的局限) |