十一:JAVA核心,适合提高-----Java语言进阶之抽象类和接口

抽象类

JAVA创建出的一种专门做父类的类。
抽象类的本质还是类,只是内部新增了抽象方法。

抽象类的基本概念

抽象方法是只声明而未实现的方法,所有抽象方法必须使用abstract关键字声明,包含抽象方法的类也必须使用abstract class声明。
1.抽象类和抽象方法必须用abstract关键字修饰
2.抽象类不能直接实例化,也不能用new关键字产生对象
3.抽象方法只需要声明,不需要实现
4.含有抽象方法的类必须被声明为抽象类,抽象类的子类必须覆写所有抽象方法才能实例化,否则子类还是抽象类。
5.抽象类不能用final(抽象类必须有子类,final不能有子类)

abstract class 类名称
{
声明数据成员;
访问权限 返回类型 方法名(参数列表)
{
//定义一般方法
}
abstract 返回类型 方法名(参数列表)
{
//没有方法体
}
}
/**
 * @author 86152
 *练习,抽象类
 */
 abstract class person
 {
	 String name;
	 int age;
	 String occupation;
	 abstract public String talk();
 }
 class student extends person
 {
	 student(String name,int age,String occupation)
	 {
		 this.name=name;
		 this.age=age;
		 this.occupation=occupation;
	 }
	 public String talk()
	 {
		 System.out.println();
		 return "学生:"+name+" .年龄:"+age+" .职业:"+occupation;
	 }
 }
 class worker extends person
 {
	 worker(String name,int age,String occupation)
	 {
		 this.name=name;
		 this.age=age;
		 this.occupation=occupation;
	 }
	 public String talk()
	 {
		 System.out.println();
		 return "工人"+name+" .年龄:"+age+" .职业:"+occupation;
	 }
 }
public class demo {
	public static void speak(String arr)
	{
		System.out.println(arr);
	}
	public static void main(String []args)
	{
		student stu=new student("小红",20,"学生");
		worker wor=new worker("小李",20,"工人");
		speak(stu.talk());
		speak(wor.talk());
	}


}

在这里插入图片描述

/**
 * @author 86152
 *练习,抽象类应用
 */
 abstract class action    //抽象类(包括了全局常量,抽象方法,普通方法)
 {
	final static int EAT=1;    //全局常量
	final static int WORK=2;
	final static int RUN=5;
	final static int SLEEP=10;
	 
	 abstract public void eat();//抽象方法
	 abstract public void work();
	 abstract public void run();
	 abstract public void sleep();
	 
	 public void choose(int i)
	 {
		 switch(i)
		 {
		 case EAT:
			 eat();
			 break;
		 case WORK:
			 work();
			 break;
		 case RUN:
			 run();
			 break;
		 case SLEEP:
			 sleep();
			 break;
		 case EAT+WORK:
			 eat();
		     work();
		     break;
		 case EAT+RUN+SLEEP:
			 eat();
		     run();
		 	sleep();
		 	break;
		 case EAT+WORK+RUN+SLEEP:
	 		 eat();
		 	work();
		 	run();
		 	sleep();
		 	break;
		 default:
			 System.out.println("条件不符");
		 }		 
	 }
 }
 class robot extends action               //类继承了抽象类
 {
	 public void eat()                            //实现抽象类里所有的抽象方法
	 {
		 System.out.println("-----机器人加油---------");
	 }
	 public void work()
	 {
		 System.out.println("-----机器人工作---------");
	 }
	 
	 public void run()
	 {}
	 public void sleep()
	 {}
 }
 class beauty extends action
 {
	 public void eat()
	 {
		 System.out.println("-----美女吃饭---------");
	 }
	 public void work()
	 {	 
	 }
	 public void run()
	 {
		 System.out.println("-----机器人跑步---------");
	 }
	 public void sleep()
	 {
		 System.out.println("-----机器人休眠---------");
	 }
 }
 class smart extends action
 {
	 public void eat()
	 {
		 System.out.println("-----帅哥吃饭---------");
	 }
	 public void work()
	 {
		 		 System.out.println("-----帅哥吃饭---------");
	 }
	 public void run()
	 {
		 System.out.println("-----帅哥跑步---------");
	 }
	 public void sleep()
	 {
		 System.out.println("-----帅哥休眠---------");
	 }
 }
 
public class demo {
	
	public static void main(String []args)
	{
		action ro=new robot();    //用子类实例化父类,实现了多态
		action bea=new beauty();
		action sma=new smart();
		ro.choose(action.RUN);
		bea.choose(action.RUN);
		sma.choose(action.WORK);
	}


}

在这里插入图片描述

接口

接口也是一种特殊的类

接口的基本概念

1.使用interface关键字来定义接口
2.接口的数据成员必须初始化,且数据成员均为常数,常见的是全局变量
3.接口的方法是abstract(不能像抽象类一样定义一般方法)

interface 接口名称
{
final 数据类型 数据成员名称=常量;
abstract 返回类型 方法名(参数类型);//抽象方法
default 返回类型 方法名(参数类型)//带方法体的默认方法
{
//方法体
}
}

接口的使用原则

1.接口必须有子类
2.子类依靠implements关键字实现多个接口
3.子类必须覆写接口的全部抽象方法
4.接口可以利用对象多态性,利用子类实现对象的实例化

/**
 * @author 86152
 *练习,接口的实现
 */
interface interfaceA {         //使用关键字interface定义一个接口
public static final String info="hello world";   //接口的数据成员必须初始化成常数(常见是全局变量static)
public static String info2="GRAET";              //final关键字可以省略
public void print();                      //因为接口中只有抽象方法和默认方法,因此抽象方法可以不加关键字abstract
public default void print2()              //默认方法,默认的方法实现,子接口不需要每次都写一些共有方法
{
	System.out.println(info2+"接口默认方法");
	}
}
class B implements interfaceA  //接口的实现
{
	public void print()
	{
		System.out.println(info+"------B覆写接口的抽象方法");
	}
	
	}
class C implements interfaceA  //接口的实现
{
	public void print()
	{
		System.out.println(info+"-------C覆写接口的抽象方法");
	}
	
	}
public class demo {
	
	public static void main(String []args)
	{
		B b=new B();
		C c=new C();
		b.print();
		b.print2();
		c.print();
		c.print2();
	}


}

在这里插入图片描述

/**
 * @author 86152
 *练习,子类实例化父接口
 */
interface interfaceA {         //使用关键字interface定义一个接口
public static final String info="hello world";   //接口的数据成员必须初始化成常数(常见是全局变量static)
public static String info2="GRAET";              //final关键字可以省略
public void print();                      //因为接口中只有抽象方法和默认方法,因此抽象方法可以不加关键字abstract
public default void print2()              //默认方法,默认的方法实现,子接口不需要每次都写一些共有方法
{
	System.out.println("------A接口默认方法");
	}
}
interface interfaceB{
	public void print3();
	public default void print2()              //默认方法,默认的方法实现,子接口不需要每次都写一些共有方法
	{
		System.out.println("-----B接口默认方法");
		}
	
}
class C implements interfaceA,interfaceB  //接口的实现
{
	public void print()
	{
		System.out.println(info+"-------覆写接口A 的抽象方法");
	}
	public void print3()
	{
		System.out.println(info+"-------覆写接口B的抽象方法");
	}
	@Override
	public void print2() {                    //覆写同名的默认方法,会产生二义性
		// TODO Auto-generated method stub
		interfaceB.super.print2();
	}
	}
public class demo {
	
	public static void main(String []args)
	{
		
		C c=new C();//初始化子类对象
		interfaceB b=c;//为父接口实例化
		interfaceA a=c;
		b.print3();
		b.print2();
		a.print();
		a.print2();
	}


}

在这里插入图片描述

接口的作用

1.制定标准

/**
 * @author 86152
 *练习,接口作用,制定标准
 */
interface USB
{
	public void work();
	
}
class PRINT implements USB
{
 public void work()
 {
	 System.out.println("------打印机开始工作-------");
	 System.out.println("------USB模式-------");
 }

}
class FLASH implements USB
{
	public void work()
	 {
		System.out.println("------U盘开始工作-------");
		 System.out.println("------USB模式-------");
	 }
	
}
class computer
{
	
	public  void computer(USB usb)
	{
		usb.work();;
		
	}
}
public class demo {
	
	public static void main(String []args)
	{
		
		computer c=new computer();
		c.computer(new PRINT());
		c.computer(new FLASH());
	}


}

在这里插入图片描述
2.工厂模式

/**
 * @author 86152
 *练习,接口作用,工厂模式
 */
interface FRUIT
{
	public void eat();
	
}
class APPLE implements FRUIT
{
 public void eat()
 {
	 System.out.println("------吃苹果-------");
 }

}
class ORANGE implements FRUIT
{
	public void eat()
	 {
		System.out.println("------吃橘子-------");
	 }
	
}
class factory //所有的接口对象都通过工厂取得!!!!!!
{
	public static FRUIT getinstance(String classname)
	{
		if(classname.equals("APPLE"))
		{
			return new APPLE();
		}
		if(classname.equals("ORANGE"))
		{
			return new ORANGE();
			
		}
		return null;
	}
	}

public class demo {
	
	public static void main(String []args)
	{
		
		FRUIT f=factory.getinstance("APPLE");
		f.eat();
	}
}

在这里插入图片描述

抽象类和接口的对比

两者在定义的语法,组成,访问权限,使用的方法,两者的关系,设计的模式,局限性等方面都有不同
1.抽象类的定义语法是abstract class,接口的定义语法是interface

2.抽象类可以由常量,全局常量,变量,构造方法,普通方法,抽象方法构成
接口由全局常量,抽象方法,默认方法构成

3.抽象类可以使用各种权限,接口只能使用public

4.子类通过extends关键字继承抽象类,通过implements关键字继承接口

5.一个抽象类可以实现多个接口,一个接口可以继承多个接口

6.抽象类有单继承局限,接口没有单继承局限

(在实际应用中,抽象类和接口常常可以转换使用,更建议使用接口,因为接口没有单继承性)

练习

/**
 * @author 86152
 *练习,1. 设计一个限制子类的访问的抽象类实例,
 *要求在控制台输出如下结果。
教师→姓名:刘三,年龄:50,职业:教师
工人→姓名:赵四,年龄:30,职业:工人

 */
abstract class person
{
private String name;
private int age;
private String occupation;
 person()
{
	name="管理员";
	age=18;
	occupation="网络工程师";
}
public person(String name,int age,String occupation)
{
	this.name=name;
	this.age=age;
	this.occupation=occupation;
	//System.out.println("教师-->姓名:"+name+",年龄:"+age+",职业:"+occupation);
}
public  String get_name()
{
	return this.name;
	}
public  String get_occup()
{
	return this.occupation;
	}
public  int get_age()
{
	return this.age;
	}
public  void set_name(String name)
{
	this.name=name;
	}
public  void set_occup(String occupation)
{
	this.occupation=occupation;
	}
public  void set_age(int age)
{
	this.age=age;
	}
abstract void say();

}

class teacher extends person
{
	teacher(String name,String occupation,int age)
	{
		super(name,age,occupation);
		
	}
	teacher()
	{
		set_name("刘三");
		set_age(50);
		set_occup("教师");
	}
	void say()
	{
		System.out.println("教师->姓名:"+super.get_name()+",年龄:"+super.get_age()+",职业:"+super.get_occup());
	}
}
class worker extends person
{
	worker(String name,String occupation,int age)
	{
		super(name,age,occupation);
	}
	worker()
	{
		set_name("赵四");
		set_age(30);
		set_occup("工人");
	}
	void say()
	{
		System.out.println("工人->姓名:"+super.get_name()+",年龄:"+super.get_age()+",职业:"+super.get_occup());
	}
}
class factory{
	public static person get_factory(String info){
		switch(info)
		{
		case "WORKER":
			return new worker();
			
		case "TEACHER":
			return new teacher();
		default:
			return null;
		}
	}
}
public class demo {
	
	public static void main(String []args)
	{
		
		 person someone2=factory.get_factory("TEACHER");
		 someone2.say();
		 person someone=factory.get_factory("WORKER");
			someone.say();
		//worker ming=new worker("ming","worker",18);//展示不通过工厂为对象赋值
		//ming.say();
		//person someone3=ming;
	//	someone3.say();
		
	}
}

在这里插入图片描述

/**
 * @author 86152
 *练习,.利用接口及抽象类设计实现
⑴ 定义接口圆形CircleShape(),其中定义常量PI,默认方法area计算圆面积;
⑵ 定义圆形类Circle实现接口CircleShape,包含构造方法和求圆周长方法;
⑶ 定义圆柱继承Circle实现接口CircleShape,包含构造方法,圆柱表面积,体积;
⑷ 从控制台输入圆半径,输出圆面积及周长;
⑸ 从控制台输入圆柱底面半径及高,输出圆柱底面积、圆柱表面积及体积。
 */
interface circleshape
{
	public static final double PI=3.14;
	public double area();
}
class circle implements circleshape
{
	public double rad;
	circle(){this.rad=1.0;
	System.out.println("圆形的半径是:"+rad);}
	
	circle(double rad)
	{
		this.rad=rad;
		System.out.println("圆形的半径是:"+rad);
	}
	public double area()
	{
		return rad*rad*PI;
	}
}
class circle2 implements circleshape
{
	public double rad;
	public double height;
	circle2(){this.rad=1.0;
	this.height=1;
	System.out.println("圆柱形的半径是:"+rad+"圆柱形的高是:"+height);}
	
	circle2(double rad,double height)
	{
		this.rad=rad;
		this.height=height;
		System.out.println("圆柱形的半径是:"+rad+"圆柱形的高是:"+height);
	}
	public double area()
	{
		return 2*rad*rad*PI+2*rad*PI*height;
	}
	public double volume()
	{
		return rad*rad*PI*height;
	}
}
public class demo {
	public static void print(double arr)
	{
		System.out.println(arr);
	}
	public static void main(String []args)
	{
		circle c=new circle(3);
		print(c.area());
		circle2 c2=new circle2(3,2);
		print(c2.area());
		print(c2.volume());
	}
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值