大数据java篇——类的继承

java只支持单继承,但支持多层继承

父类中的私有内容不能被继承

在一个类中调用父类方法用 "super.",调用父类构造函数用"super( )" 。调用本类方法用"this.",调用本类构造函数用"this( )"

函数覆盖

1.子类中出现与父类一模一样的方法时,会出现覆盖操作

2.父类中的私有方法不可以被覆盖

3.在子类覆盖方法中,继续使用被覆盖的方法可以通过super函数名获取

4.覆盖注意事项

    覆盖时,子类方法权限一定要大于等于父类权限

    静态只能覆盖静态

子类的实例化过程

1.子类中所有的构造函数默认都会访问父类中空参数的构造函数

2.每一个构造函数的第一行都有默认语句super( )

3.当父类中没有空参数的构造函数时,子类的构造函数必须通过this或者super语句指定要访问的构造函数。

final关键字

1.final关键字可以修饰类,方法,变量;

2.final修饰的类不可以被继承;

3.final修饰的方法不可以被覆盖;

4.final修饰的变量是一个常量,只能被赋值一次;

5.内部类只能访问被final修饰的局部变量。

抽象类abstract

抽象类不可以被实例化

1.没有方法体的存在,必须使用abstract修饰;

2.抽象方法只能存在于抽象类中,但抽象类中可以不包含抽象方法;

3.抽象类必须使用abstract修饰;

4.非法组合:

    abstract+static    abstract+final    abstract+private

接口

接口之间支持多继承

接口中的成员修饰符都是固定的:

1.成员变量:public static final

2.成员函数:public abstract

3.接口中的成员都是public的

接口与抽象类:

1.抽象类体现继承关系,一个类只能单继承;接口体现实现关系,一个类可以多实现

2.抽象类是继承,是“is a”的关系;接口是实现,是“like a”的关系

3.抽象类可以定义非抽象方法,供子类直接使用;接口的方法都是抽象,接口中的成员都有固定修饰符

public class GetMarry 
{

	public static void main(String[] args) 
	{
		RichMan man=new RichMan();
		WomenStar woman=new WomenStar();
		man.marry(woman);
	}

}

interface IWrite
{
	public void write();
}

interface IRich
{
	public void rich();
}

interface IBeauty
{
	public void beauty();
}

interface IWRB extends IWrite,IRich,IBeauty
{
}

class WomenStar implements IWRB
{
	public void write ()
	{
		System.out.println("I'm write");
	}
	public void rich()
	{
		System.out.println("I'm rich");
	}
	public void beauty()
	{
		System.out.println("I'm beauty");
	}
}

class RichMan
{
	public void marry(IWRB woman)
	{
		System.out.println("聊聊看");
	}
}

适配器模式

如果接口中的方法较多,而使用接口的时候却只关心其中的一个或某几个方法,如果采用常规手段,就需要对每个方法都加以实现,此时可以创建一个抽象类来实现接口,对不关心的方法进行空实现,对需要的方法保持不变,然后新建类继承该抽象类,只需对关心的方法进行复写即可。

多态

程序中体现:

父类或接口的引用指向自己的子类对象,如果要访问子类中的特有方法,需向下强制转型,如:

Animal a=new Cat( );

Cat c=(Cat) a;

好处:

提高了程序的扩展性和后期可维护性,前期定义的代码可以使用后期的内容

前提:

需存在继承或者实现,需要有覆盖操作

public class DuoTai 
{

	public static void main(String[] args) 
	{
		Jing8 d1=new Jing8();
		d1.name="大黄";
		System.out.println(d1.name);
		
		Dog d2=new Jing8();
		d2.name="大灰";
		System.out.println(d2.name);
		
		Animal d3=new Jing8();
		d3.name="大黑";
		System.out.println(d3.name);
	}

}

class Animal
{
	public String name ;
}

class Dog extends Animal
{
	public String color;
}

class Jing8 extends Dog
{
	public String cry;
}

内部类

将一个类定义在另一个类里面,对里面的类就叫内部类。

内部类可直接访问外部类的成员,包括私有成员;外部类要访问内部类中的成员必须要建立内部类的对象。

内部类定义在成员位置上:

可被private static成员修饰符修饰;被static修饰的内部类只能访问外部类中的静态成员

内部类定义在局部位置上:

也可直接访问外部类中的成员;同时可以访问所在局部中的局部变量,但必须是被final修饰的


Outer.Inner in = new Outer().new Inner();

in.show();

当内部类是静态时,可写成:

Outer.Inner in = new Outer.Inner();

in.show();

匿名内部类对象

将对类的继承,方法重写,对象的创建集成到一行代码,省去了定义类并实例化的麻烦。也就是说,建立一个带内容的外部类或者接口的子类匿名对象。

格式为:

new 外部类名或接口名(){覆盖类或者接口中的代码,(也可自定义内容)};

//局部内部类
//定义一个接口
interface OutInterface{

}
public class OuterClass {
	public OutInterface doit(final String x){
		//在doit()方法中定义一个内部类
		class InnerClass implements OutInterface{
			InnerClass(String s){
				s=x;
				System.out.print(s);
			}
		}
		return new InnerClass("doit");
	}
}
//匿名内部类
//定义一个接口
interface OutInterface{

}
public class OuterClass {
	public OutInterface doit(){
		return new OutInterface(){    //声明匿名内部类
			private int i=0;
			public int getValue(){
				return i;
			}
		};                           //匿名内部类定义结束后需要加分号标识
	}
}

异常

Throwable 类是 Java 语言中所有错误或异常的超类。只有当对象是此类(或其子类之一)的实例时,才能通过 Java 虚拟机或者 Java throw 语句抛出。类似地,只有此类或其子类之一才可以是 catch 子句中的参数类型。 

Throwable:

Error:不可处理的

Exception:可以处理的

Error和Exception的子类名都是以其父类名作为后缀的

Throwable中的常用方法:

getMessage()    获取异常信息,返回字符串;

toString()    获取异常类名和异常信息,返回字符串;

printStackTrace()    获取异常类名和异常信息,以及异常出现在程序中的位置,返回值void;

printStackTrace(PrintStream s)    通常用该方法将异常内容保存在日志文件中,以便查阅。

关键字throws和throw:

throws用于标识函数暴露出的异常,用在函数上,后面跟异常类名;

throw用于抛出异常对象,用在函数内,后面跟异常对象;

class person{
	private int age;
	public void setAge(int age) throws Exception{
		if (age>100 || age<0){
			throw new Exception("年龄非法");
		}
		this.age=age;
	}
	public int getAge(){
		return age;
	}
}

public class a {

	public static void main(String[] args){
		person p=new person();
		try{
			p.setAge(120);
		}
		catch (Exception e){
			e.printStackTrace();
		}
	}

}
//自定义异常
class AgeTooBigException extends Exception{
	private String info;
	public AgeTooBigException(String info){
		this.info=info;
	}
	public String getInfo(){
		return info;
	}
}
//自定义异常
class AgeTooSmallException extends Exception{
	private String info;
	public AgeTooSmallException(String info){
		this.info=info;
	}
	public String getInfo(){
		return info;
	}
}

class Person {
	private int age;
	public void setAge(int age) throws AgeTooBigException,AgeTooSmallException{
		if(age>100){
			throw new AgeTooBigException("年龄过大!");
		}
		else if(age<0){
			throw new AgeTooSmallException("年龄过小!");
		}
		this.age=age;
	}
	public int getAge(){
		return age;
	}
}

public class ExceptionDemo2 {
	public static void main(String[] args) {
		Person p=new Person();
		try{
			p.setAge(101);
		}
		catch(AgeTooBigException e){
			System.out.println(e.getInfo());;
		}
		catch(AgeTooSmallException e){
			System.out.println(e.getInfo());;
		}
		System.out.println(p.getAge());;

	}

}
RuntimeException(运行时异常)可以在方法定义时不进行抛出声明,RuntimeException抛出可以不使用try-catch-finally进行捕获。
方法覆盖时不可以追加新的异常抛出声明,但可以抛出所覆盖的方法声明的异常的子类。

猜你喜欢

转载自blog.csdn.net/sp_ur/article/details/80524556
今日推荐