Java中面向对象(下)

类的继承

在Java中,类的继承是指在一个现有的类的基础上构建一个新的类,构建出来的新类被称作子类,现有的类被称作父类,子类会自动拥有父类所有继承的属性和方法。需要使用extends关键字。

Example01.java
public class Example01 {
	public static void main(String[] args) {
		Dog d=new Dog();
		d.name="沙皮狗";
		d.printName();
		d.shout();
	}
}
class Animal{
	String  name;
	void shout() {
		System.out.println("动物发出叫声");
	}
}
class Dog  extends Animal{
	     void printName() {
			 System.out.println("name="+name); 
	}
}

注意:
1.在Java中,类只支持单继承,不允许多重继承,一个类只能有一个直接父类。
2.多个类可以继承一个父类。
3.在Java中,多层继承是可以的。
4.在Java中,子类和父类是一种相对概念,也就是说一个类是某个类父类的同时,也可以是另一个类的子类。

重写父类的方法

Example02.java
public class Example02 {
	public static void main(String[] args) {
		Dog dog=new Dog();    //创建Dog类的实例对象
		dog.shout();			//调用dog重写的shout方法
	}

}
class Animal{
	String  name;
	void shout() {
		System.out.println("动物发出叫声");
	}
}
class Dog  extends Animal{
	 void shout() {
		 System.out.println("汪汪汪....");	 
   }
}

在调用Dog类对象的shout()方法时,只会调用子类重写的该方法,并不会调用父类的shout()方法。

super关键字
super关键字用于访问父类的成员
具体用法:
1.使用super关键字调用父类的饿成员变量和成员方法。

public class Example03 {
	public static void main(String[] args) {
		Dog dog=new Dog();    //创建一个Dog对象
		dog.shout();			//调用dog重写的shout方法
		dog.printName();  //调用dog对象的printName()方法
	}
}
class Animal{
	String  name="动物";
	void shout() {
		System.out.println("动物发出叫声");
	}
}
class Dog  extends Animal{
	String  name="犬类";
	//重写父类的饿shout()方法
	 void shout() {
		super.shout();  //访问父类的成员方法
	}
	 void printName() {
		 System.out.println("name="+super.name); //访问父类的成员变量
	 }
}

2.使用super关键字调用父类的构造方法

public class Example04 {
	public static void main(String[] args) {
		Dog dog=new Dog();    //实例化子类dog对象
	}
}
class Animal{
	//定义Animal类有参的构造方法
	public Animal(String name) {
		System.out.println("我是一只"+name);
	}
}
class  Dog  extends Animal{
	public Dog() {
		super("沙皮狗");
	}
}

final关键字

final关键字可用于修饰类、变量和方法,它有“这是无法改变的”或者“最终”的含义。
特性
1.final修饰的类不能被继承
2.final修饰的方法被子类重写
3.final修饰的变量(成员变量和局部变量)是常量,只能赋值一次。

final关键字修饰类
final关键字修饰的类不能被继承由此可以看出final关键字修饰的类不能被继承

final关键字修饰方法
在这里插入图片描述
正是由于final的这种特性,当在父类中定义 某个方法时,如果不希望被子类重写,就可以使用final关键字修饰该方法。

final关键字修饰变量

Java中被final修饰的变量为常量,它只能被赋值一次,也就说final修饰的变量一旦被赋值,其值不能改变。
在这里插入图片描述
再次赋值会报错。

抽象类和接口

抽象类

当一个类包含了抽象方法,该类必须使用abstract关键字来修饰,使用abstract关键修饰的类称为抽象类。

public class Example05 {
	public static void main(String[] args) {
		Dog dog=new Dog();    
		dog.shout();
	}
}
//定义抽象类Animal
 abstract class Animal{
 //定义抽象方法
	 abstract void shout();
}
class  Dog  extends Animal{
	public void shout() {
		System.out.println("汪汪汪...");
	}
}
接口

如果一个抽象类中所有的方法都是抽象的,则可以将这个类用另一个方式来定义,即接口。在定义接口时,需要使用interface关键字来声明。并使用implements关键字来实现接口中所有的方法。

public class Example06 {
	public static void main(String[] args) {
		Dog dog=new Dog();    //创建Dog类的实例对象
		dog.breathe();			//调用Dog类的breathe方法
		dog.run();				//调用Dog类的run方法
	}
}
//定义接口
interface Animal{
	int ID=1;
	void breathe();
	void run();
}
//实现Animal接口
class  Dog  implements Animal{

	@Override
	public void breathe() {
	 System.out.println("呼吸");	
	}

	@Override
	public void run() {
		System.out.println("跑步");
	}
}

接口的特点
1.接口中的方法都是抽象的,不能实例化对象。
2.当一个类实现接口时,如果这个类是抽象类,则实现接口中的部分方法即可,否则需要实现接口中所有的方法。
3.一个类通过implements关键字实现接口时,可以实现多个接口,被实现的多个接口之间需要用逗号隔开。
4.一个接口可以通过extends关键字继承多个接口,接口之间用逗号隔开。
5.一个类在继承另一个类的同时还可以实现接口,此时,extends关键字必须位于implements关键字之前。

多态

在同一方法中,这种由于参数类型不同而导致执行效果各异的现象就是多态。


public class Example07 {
	public static void main(String[] args) {
		Animal  an1=new Cat();       //创建Cat对象,使用Animal类型的变量an1引用
		Animal  an2=new Dog();       //创建Dog对象,使用Animal类型的变量an2引用
		animalShout(an1);     		//调用animalShout()方法,将an1作为参数传入
		animalShout(an2);     		//调用animalShout()方法,将an2作为参数传入
 	}
	//定义静态方法animalShout()方法,接收一个Animal类型的参数
	public static void animalShout(Animal an) {
		an.shout();  //调用实际参数的shout()方法
	}
}
//定义接口
interface Animal{
	void shout();
}
//实现Animal接口
class  Cat  implements Animal{
	public void shout() {
	 System.out.println("喵喵喵...");	
	}
}
class  Dog  implements Animal{
	public void shout() {
	 System.out.println("汪汪汪...");	
	}
}

异常

在Java语言中引入了异常,以异常类的形式对这些非正常情况进行封装,通过异常处理机制对程序运行时发生的各种问题进行处理。
Exception类称为异常类。

try…catch和finally
try {
		
	}catch(ExceptionType(Exception类及其子类) e){
		//对ExceptionType的处理
	}
public class Example08 {
	public static void main(String[] args) {
		try {
		int result=divide(4,0);
		System.out.println(result);
		}catch(Exception e) {
			System.out.println("捕获的异常信息为:"+e.getMessage());
		}
		System.out.println("程序继续");
	}
  public static int divide(int x,int y) {
	int result=x/y;
	return result;
}
}

在程序中,有时候我们希望有些语句是否发生异常都执行,这时就可以在try…catch语句后加一个finally代码块。

public class Example09 {
	public static void main(String[] args) {
		try {
		int result=divide(4,0);
		System.out.println(result);
		}catch(Exception e) {
			System.out.println("捕获的异常信息为:"+e.getMessage());
		}finally {
			System.out.println("进入fianlly代码块");
		}
		System.out.println("程序继续");
	}
  public static int divide(int x,int y) {
	int result=x/y;
	return result;
}
}

throws关键字
throws关键字需要写在方法声明的后面,throws后面需要声明方法中发生异常的类型,通常将这种做法称为方法声明抛出一个异常

public class Example10 {
	public static void main(String[] args) {
	
		int result;
		try {
			result = divide(4,2);
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
  public static int divide(int x,int y)throws Exception {
	int result=x/y;
	return result;
}
}

如果不知道如何处理声明抛出的异常,也可以使用throws关键字继续将异常抛出,这样的程序也能编译通过,但需要注意的是,程序一旦发生异常,如果没有被处理,程序就会非正常停止。
1.编译时异常
在Java中,Exception类中除了RuntimeException类及其子类都是编译时异常。
2.运行时异常
RuntimeException类及其子类都是运行时异常。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值