Java 学习 day3

方法重写

public class Application {
	public static void main(String[] args) {}
		// 方法的调用之和左边,定义的数据类型有关
		A a = new A();
		a.test(); // A
		
		// 父类的引用指向了子类对象
		B b = new A();
		b.test(); // B
}
// 继承
public class A extends B {
	public static void test() {
		System.out.println("A => test()");
	}
}
// 重写都是方法的重写,和属性无关
public class B {
	public static void test() {
		System.out.println("B => test()");
	}
}

重写:需要有继承关系,子类重写父类的方法

  • 方法名必修相同
  • 参数列表必须相同
  • 修饰符:范围可以扩大:public > protected > default > private
  • 抛出的异常:范围,可以被缩小,但不能扩大;ClassNotFoundException–>Exception(大)

重写,子类的方法和父类必须要一致;方法体不同!

为什么需要重写:

  • 父类的功能,子类不一定需要,或者不一定满足!
  • Alt + Insert : Override;

多态

public class Application {
	public static void main(String[] args) {
		// 一个对象的实际类型是确定的
		// new Student();
		// new Person();

		// 可以指向的引用类型就不确定了:父类

		// Student 能调用的方法都是自己的或者继承父类的!
		Student s1 = new Student();
		// Person 父类型,可以指向子类,但是不能调用子类独有的方法
		Person s2 = new Student();
		Object s3 = new Student();

		// 对象能执行哪些方法,主要看对象左边的类型,和右边关系不大!
		s2.run(); // 子类重写了父类的方法,执行子类的方法
		((Student)s2).eat(); // 子类独有的方法,父类不能调用,要强制转换为子类
		s1.run();
	}
}
public class Person {
	public void run() {
		System.out.println("run");
	}
}

多态的注意事项:

  1. 多态是方法的多态,属性没有多态
  2. 父类和子类,有联系:类型转换异常! ClassCastException!
  3. 存在条件:继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new Son();
  1. static 方法,属于类,它不属于实例
  2. final 常量
  3. private 方法
// 继承
public class Student extends Person {
	@Override
	public void run() {
		System.out.println("son");
	}
	public void eat() {
		System.out.println("eat");
	}
}

instanceof 和 类型转换

instanceof

public class Application {
	public static void main(String[] args) {
		// Object > Person > Teacher
		// Object > Person > Student
		// Object > String
		Object object = new Student();
		System.out.println(object instanceof Student); // True
		System.out.println(object instanceof Person); // True
		System.out.println(object instanceof Object); // True
		System.out.println(object instanceof Teacher); // False
		System.out.println(object instanceof String); // False
		
		//==================================================================
		
		// Object > Person > Teacher
		// Object > Person > Student
		// Object > String
		Person person = new Student();
		System.out.println(person instanceof Student); // True
		System.out.println(person instanceof Person); // True
		System.out.println(person instanceof Object); // True
		System.out.println(person instanceof Teacher); // False
		System.out.println(person instanceof String); // 编译报错!

		//==================================================================
		
		// Object > Person > Teacher
		// Object > Person > Student
		// Object > String
		Student student = new Student();
		System.out.println(student instanceof Student); // True
		System.out.println(student instanceof Person); // True
		System.out.println(student instanceof Object); // True
		System.out.println(student instanceof Teacher); // 编译报错!
		System.out.println(student instanceof String); // 编译报错!
	}
}

类型转换

public class Application {
	public static void main(String[] args) {
		// 类型之间的转换: 父  子

		//  高                  低
		Student student = new Student();
		// student将这个对象转换为Student类型,我们就可以使用Student类型的方法了!
		// 子类转换为父类,可能丢失自己本来的一些方法!
		Student.go();
		Person person = student; 
		
	}
}
  1. 父类引用指向子类的对象
  2. 把子类转换为父类,强制转换!可能会丢失部分方法!
  3. 把父类转换为子类,自动转换!
  4. 方便方法的调用,减少重复的代码!简洁!

static关键字详解

// static
public class Student {
	private static int age; // 静态的变量
	private double score; // 非静态的变量
	
	public void run() {
		
	}
		
	public static void go() {
		
	}
	
	public static void main(String[] args) {
		Student s1 = new Student();
		
		System.out.println(Student.age);
		System.out.println(s1.age);
		System.out.println(s1.score);

		//====================================================
		
		new Student().run();
		go();
	}
}

静态代码块

public class Person {
	// 2
	{
		// 代码块(匿名代码块)
		System.out.println("匿名代码块");
	}
	// 1
	static {
		// 静态代码块
		System.out.println("静态代码块");
	}
	// 3
	public Person() {
		System.out.println("构造方法");
	}
}
// 静态导入包
import static java.lang.Math.random
import static java.lang.Math.PI

public class Test {
	public static void main(String[] args) {
		System.out.println(random);
		System.out.println(PI);
	}
}

抽象类

abstract

// abstract 抽象类:类     extends:单继承       接口可以多继承
public abstract class Action {
	// 约束,有人帮我们实现
	// abstract,抽象方法,只有方法名,没有方法的实现!
	public abstract void doSomething();

}
  1. 不能new这个抽象类,只能靠子类去实现它;约束!
  2. 抽象类中可以写普通的方法
  3. 抽象方法必须在抽象类中
  4. 抽象的抽象:约束!
// 抽象
// 抽象类的所有方法,继承了他的子类,都必须要实现他的方法! 除非
public class A extends Action {
	@Override
	public void doSomething();
}

接口

// interface 定义的关键字
public interface UserService {
	// 常量
	public static final int AGE = 99;
	
	// 接口中的所有定义的方法其实都是抽象的public abstract
	void run(String name);
	void delete(String name);
	void update(String name);
	void query(String name);
}
public interface TimeService {
	void timer();
}
  • UserServiceImpl
// 抽象类: extends
// 类 可以实现接口 implement 接口
// 实现了接口的类,就需要重写接口中的方法
// 利用接口多继承
public class UserServiceImpl implements UserService,TimeService {
	@Override
	public void add(String name) {
		
	}
	@Override
	public void delete(String name) {
		
	}
	@Override
	public void update(String name) {
		
	}
	@Override
	public void query(String name) {
		
	}
	@Override
	public void timer() {
		
	}
	
}

N种内部类

public class Outer {
	private int id = 10;
	public void out() {
		System.out.println("这是外部类的方法")}
	
	public static class Inner {
		public void in() {
			System.out.println("这是内部类的方法")}
		
		// 获得外部类的私有属性
		public void getID() {
			System.out.println(id);
		}
		
	}
}
import Outer;

public class Application {
	public static void main(String[] args) {
		Outer outer = new Outer();
		// 通过这个外部类来实例化内部类
		Outer.Inner inner = outer.new Inner();
		inner.getID();
	}
}

异常

在Exception分支中有一个重要的子类RuntimeException(运行时异常)

  • ArrayIndexOutOfBoundsException(数组下标越界)
  • NullPointerException(空指针异常)
  • ArithmeticException(算术异常)
  • MissingResourceException(丢失资源)
  • ClassNotFoundException(找不到类)等异常,这些异常是不检查异常,程序中可以选择捕获处理,也可以不处理。
public class Test {
	public static void main(String[] args) {
		int a = 1;
		int b = 0;
		
		// 假设要捕获多个异常:从小到大
		try { // try 监控区域
			
			if(b == 0) { // throw  throws
				throw new ArithmeticException(); // 主动抛出异常
			}
			
			System.out.println(a / b);
		}catch (Error e) { // catch(想要捕获的异常类型) 捕获异常
			System.out.println("程序出现异常,变量b不能为0")}catch () {
			System.out.println("Exception")}catch (Throwable t) {
			System.out.println("Throwable")}finally { // 处理善后工作
			System.out.println("finally");
		}
		
		// finally 可以不要finally,假设IO,资源,关闭
	}
	public void a() {
		b();
	}
	public void b() {
		a();
	}
}

Ctrl + Alt + T : 快速生成异常语句

public class Test {
	public static void main(String[] args) {
		try {
			new Test().test(1, 0);
		}catch (ArithmeticException e) {
			e.printStackTrace();
		}
		
		// 假设这方法中,处理不了这个异常。方法上抛出异常
		public void test(int a, int b) throws ArithmeticException {
			if(b == 0) { // throw  throws
				throw new ArithmeticException(); // 主动抛出异常
			}
		}
	}
}
public class Test {
	// 可能存在的异常
	static void test(int a) throws MyException {
		System.out.println("传递的参数为:" + a); 
		if (a > 10) {
			throw new MyException(a); // 抛出异常
		}
		System.out.println("OK");
	}
	public static void main(String[] args) {
		try {
			test(1);
		} catch (MyException e) {
			// 可以增加一些处理异常的代码
			System.out.println("MyException => " + e);
		}
	}
}

自定义的异常类

public class MyException extends Exception {
	// 传递的数字 > 10;
	private int detail;
	public MyException(int a) {
		this.detail = a;
	}

	// toString:异常的打印信息
	@Override
	public String toString() {
		return "MyException{" + detail + "}";
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值