Java学习笔记(六)
一.多态
动态编译:可拓展性更强
同一方法可以根据发送对象的不同而采取多种不同行为方式
多态存在条件:
有继承关系
子类重写父类方法
父类引用指向子类对象
注意: 多态是方法的多态,属性没有多态性
package com.oop.Demo08;
public class Person {
public void run(){
System.out.println("run");
}
}
package com.oop.Demo08;
public class Student extends Person{
public void run(){
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
package com.oop.Demo08;
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的,
//可以指向的引用类型就不确定了:父类的引用指向子类
//子类Student能调用的方法都是自己的或者继承父类的
Student s1 = new Student();
//父类Person,可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
s2.run();//子类重写了父类的方法,执行子类方法
s1.run();
//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大!
s1.eat();
//s2.eat();
}
}
out:
son
son
注意点:
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系 类型转换异常 ClassCastException
3.存在条件:继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new son();
不能重写:
1.static 方法,属于类,它不属于实例
2.final 常量
3.private方法
instance of :引用类型,判断一个对象是什么类型
package com.oop.Demo09;
public class Person {
public void run(){
System.out.println("run");
}
}
package com.oop.Demo09;
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
package com.oop.Demo09;
public class Teacher extends Person{
}
package com.oop.Demo09;
public class Application {
public static void main(String[] args) {
//类型之间转化: 父 子
//高
Person obj = new Student();
//obj,我们就可以使用Student类型的方法了
//Student student = (Student)obj;
//student.go();
((Student)obj).go();
//子类转化为父类,可能会丢失自己本来的一些方法
}
}
//instanceof:
/*
//Object > String
//Object > Person > Teacher
//Object > Person > Student
Object object = new Student();
//System.out.println(x instanceof y); x,y有父子关系,编译能通过!
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
System.out.println("-------------------------------");
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);//编译报错
System.out.println("-------------------------------");
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(person instanceof String);//编译报错
*/
注意:
1.父类引用指向子类对象
2.把子类转换为父类,向上转型;
3.把父类转换为子类,向下转型;强制转换
4.方便方法的调用,减少重复的代码!简洁
抽象: 封装 继承 多态!
二.static关键字
package com.oop.Demo10;
public class Person {
//匿名代码块 2 赋初始值
{
System.out.println("匿名代码块");
}
//静态代码块 1 只执行一次
static
{
System.out.println("静态代码块");
}
//构造代码块 3
public Person(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Person p1 = new Person();
System.out.println("======");
Person p2 = new Person();
}
}
package com.oop.Demo10;
public class Student {
private static int age;//静态的变量 多线程
private double score;//非静态的变量
public void run(){//非静态方法可以调用静态方法
go();
}
public static void go(){
}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(Student.age);
System.out.println(s1.score);
System.out.println(s1.age);
go();//可调用静态方法
}
}
package com.oop.Demo10;
//静态导入包
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修饰,为抽象类
package com.oop.Demo11;
//abstract 抽象类:lei 类 extends:单继承 (接口多继承)
public abstract class Action {
//约束 有人帮我们实现
//abstract 抽象方法,只有方法名字,没有方法实现!
public abstract void doSomething();
}
package com.oop.Demo11;
//抽象类的所有方法,继承了它的子类,都必须要实现他的方法,除非子类也是抽象的
public class A extends Action{
public void doSomething(){
}
}
注意: 抽象的抽象:约束
1.不能new这个对象,只能靠子类去实现它;约束!
2.抽象类中可以写普通方法
3.抽象方法必须写在抽象类中
四.接口
普通类:只有具体实现
抽象类:具体实现和规范(抽象方法)都有
接口:只有规范!自己无法写方法-专业的约束!约束和实现分离:面向接口编程!
声明类的关键字是class,声明接口的关键字是interface
package com.oop.Demo12;
//抽象思维
//interface 定义的关键字,接口都需要实现类
public interface UserService {
//接口中的所有定义其实都是抽象的 public
void add(String name);
void delete(String name);
void update(String name);
void query(String name);
//属性默认常量 public static final
public static final int age = 99;
}
package com.oop.Demo12;
public interface TimeService {
void timer();
}
package com.oop.Demo12;
//抽象类:extends
//类可以实现接口 implements 接口
//实现了接口的类,就需要重写接口中的方法
//多继承-利用接口实现多继承
public class UserServiceImpl implements UserService,TimeService{
public void add(String name) {
}
public void delete(String name) {
}
public void update(String name){
}
public void query(String name) {
}
public void timer() {
}
}
作用:
1.约束
2.定义一些方法,让不同的人实现
3.方法都是public abstract
4.常量都是public static final
5.接口不能被实例化,接口中没有构造方法
6.implements可以实现多个接口
7.实现了接口的类,就需要重写接口中的方法
五.内部类
在一个类内部定义一个类
1.成员内部类
2.静态内部类
3.局部内部类
4.匿名内部类
package com.oop.Demo13;
public class Outer {
private int id = 10;
public void out(){
System.out.println("外部类方法");
}
public class Inner{
public void In(){
System.out.println("内部类方法");
}
//获得外部类私有属性
public void getId(){
System.out.println(id);
}
}
}
package com.oop.Demo13;
public class Application {
public static void main(String[] args) {
//new 关键字
Outer outer = new Outer();
//通过外部类来实例化内部类
Outer.Inner inner = outer.new Inner();
inner.In();
inner.getId();
}
}
六.异常机制
1.检查性异常
2.运行时异常
3.错误ERROR
异常处理机制
抛出异常
捕获异常
异常处理的五个关键字:try,catch,finally,throw,throws
package com.exception;
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
//finally 可以不要
try{//try监控区域
System.out.println(a/b);
}catch(ArithmeticException e){//catch捕获异常
System.out.println("ERROR,b不能为0");
}finally {//处理善后工作,异常不异常都会执行
System.out.println("finally");
}
try{//try监控区域
new Test().a();
}catch(ArithmeticException e){//catch(想要捕获的异常类型)捕获异常
System.out.println("ERROR,b不能为0");
}finally {//处理善后工作,异常不异常都会执行
System.out.println("finally");
}
}
public void a(){b();}
public void b(){a();}
}
out:
ERROR,b不能为0
finally
finally
七.自定义异常
用户自定义异常类,只需要继承Exception类。
步骤:
1.创建自定义异常类
2.在方法中通过throe抛出异常对象
3.在当前抛出异常的方法中处理异常,try-catch捕获并处理;否则在方法生命出通过throws关键字指明要抛出给方法调用者的异常,继续进行下一步操作。
4.在出现异常方法的调用者中捕获并处理异常
package com.exception.Demo02;
//自定义的异常类
public class MyException extends Exception{
//传递数字>10;
private int detail;
public MyException(int a){
this.detail = a;
}
//toString:异常的打印信息
public String toString(){
return "MyException{" +
"detail=" + detail +
'}';
}
}
package com.exception.Demo02;
public class Test {
//可能会存在异常的方法
static void test(int a) throws MyException{
System.out.println("the number is:" +a);
if(a>10){
throw new MyException(a);//抛出
}
System.out.println("ok");
}
public static void main(String[] args){
try{
test(11);
}catch(MyException e){
System.out.println("MyException=>"+e);
}
}
}
out:
the number is:11
MyException=>MyException{detail=11}
总结:
处理运行时异常时,采用逻辑去合理规避同时辅助try-catch处理
在多重catch块后面,可以加一个catch(Exception)来处理可能会被遗漏的异常
对于不确定的代码,也可以加上try-catch,处理潜在异常
尽量去处理异常,切忌只是简单地调用printStackTrace()去打印输出
具体如何处理异常,要根据不同的业务需求和异常类型去决定
尽量添加finally语句块去释放占用的资源