[java学习笔记]-面向对象(2)


每一个java类都是一个.java文件


多态

动态编译

//Application
public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();
        Person s2 = new Student();
        Object s3 = new Student();
        s2.run();
    }
}

//Person
public class Person {
    public void run(){
        System.out.println("Person");
    }
}

//Student
public class Student extends Person{

}

输出

输出

修改代码 重写 run() 方法

//Application
public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();
        Person s2 = new Student();
        Object s3 = new Student();
        s2.run();
        s1.run();
    }
}

//Student
public class Student extends Person{
    @Override
    public void run() {
        System.out.println("Student");
    }
}

运行输出

在这里插入图片描述

s2.run()//子类重写父类的方法,执行子类的方法

添加 eat() 方法

//Student
public class Student extends Person{
    @Override
    public void run() {
        System.out.println("Student");
    }
    public void eat(){
        System.out.println("eat");
    }
}

//Application
public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();
        Person s2 = new Student();
        Object s3 = new Student();
    
         s2.eat();    //报错
    }
}

在这里插入图片描述

Person s2 = new Student();
s2.eat();
//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
Student s1 = new Student();
    //Student 能调用的方法都是自己的或者继承父类的
Person s2 = new Student();
    //Person 父类型,可以指向子类,但不能调用子类独有的方法
Object s3 = new Student(); 

多态注意事项
1.多态是方法的多态,属性没有多态
2.父类和子类有联系,类型转换异常! ClassCastException!
3.存在条件:继承关系,方法需要重写,父类的引用指向子类对象! Father f1 = new Son();


Instanceof 类型转换

//Application
public class Application {
    public static void main(String[] args) {
     
        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
    }
}
//Person.java
public class Person {

}

//Teacher.java
public class Teacher extends Person{
}

//Student.java
public class Student extends Person{
}

运行得到

在这里插入图片描述

因为
Object > Person > Student
Object > Person > Teacher
Object > String

继续尝试

//Application
public class Application {
    public static void main(String[] args) {

        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
        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(student instanceof String);   //报错
    } 
}

在这里插入图片描述

System.out.println(X instanceof Y);
XY 有关系

转换类型

//Application
public class Application {
    public static void main(String[] args) {
        //类型之间的转换
        //高                 低
        Person Muz1 = new Student();
        //将这个对象转换为Student类型,我们可以使用Student类的方法
        
        Student muz1 = (Student) Muz1;
        muz1.go();
        
        ((Student) Muz1).go();
    }
}
//Student
public class Student extends Person{
    public void go(){
        System.out.println("go");
    }
}

运行

在这里插入图片描述

Student muz1 = (Student) Muz1;
 muz1.go();
        
((Student) Muz1).go();

//两种转换类型的格式

static

静态的属性

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

在这里插入图片描述

静态的方法

//Demo
public class Student {

    public void run(){
        System.out.println("run");
    }
    public static void go(){
        System.out.println("go");
    }
    public static void main(String[] args) {

        new Student().run();    //非静态方法
        go();                    //静态方法
    }
}
//run
//go

静态代码块

public class Person {
    {
        System.out.println("匿名代码块");
    }
    static {
        System.out.println("静态代码块");
    }
    public Person() {
        System.out.println("构造方法");
    }
    public static void main(String[] args) {
        Person person = new Person();
    }
}

输出

在这里插入图片描述

main方法中添加代码

public static void main(String[] args) {
    Person person = new Person();
    System.out.println("=====");
    Person person1 = new Person();
}

执行输出

在这里插入图片描述

static{}静态方法 只执行一次


抽象类

abstract

//Action
//abstract 抽象类
public abstract class Action {

    //abstract,抽象方法,只有方法的名字,没有方法的实现
    public abstract void doSomething();
}
//A
//抽象类对的所有方法,继承了他的子类,都必须要实现它的方法
public class A extends Action{
    @Override
    //方法的重写
    public void doSomething() {
    }
}

类是单继承,接口可以多继承

新建一个main方法new 抽象类

//Application
public class Application {
    public static void main(String[] args) {
        new Action();
    }
}

在这里插入图片描述

只能去new 抽象类的子类


接口

关键字 interface

//Demo
public interface UserService {

}

接口中不存在方法

在这里插入图片描述

只有抽象的定义

 public interface UserService {
     //接口中的所有定义其实都是抽象的
    void go();
    public abstract void run();
}

写一个增删查改方法

//UserService 
public interface UserService {
    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);
}

写一个接口,必须要重写方法

//抽象类:extends
//接口 implements 接口
public class UserServiceImpl implements UserService{
    @Override
    public void add(String name) {
    }
    @Override
    public void delete(String name) {
    }
    @Override
    public void update(String name) {
    }
    @Override
    public void query(String name) {
    }
}

作用:
1.约束
2.定义一些方法,让不同的人实
3.public abstra
4.public static
5.接口不被实例化,接口中没有
6.implements可以实
7.必须要重写接口中的方法


内部类

Demo

//Out
public class Outer {
    private int id;
    public void out(){
        System.out.println("out");
    }
    public class Inner{
        public void in(){
            System.out.println("in");
        }
    }
}
//Application
public class Application {
    public static void main(String[] args) {
        Outer outer = new Outer();
        //通过这个外部类来实例化内部类
        Outer.Inner inner = outer.new Inner();
        inner.in();
    }
}

在这里插入图片描述

内部类获取外部类的资源 私有属性

//Out
public class Outer {
    private int id = 2022;

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

在这里插入图片描述

一个java类中可以有多个class类,但只能有一个public class类

public class Outer {

}
class A{
  
}
class B{
  
}

没有名字初始化类

public class Test {
    public static void main(String[] args) {
        //没有名字初始化类,不用实例保存
        new Apple().eat();
    }
}
class Apple{
    public void eat(){
        System.out.println("Apple");
    }
}

在这里插入图片描述


Error和Exception

检查性异常

运行时异常

错误ERROR


捕获和抛出异常

try
catch
finally
throw
throws

Demo写一段报错代码

//Test
public class Test {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        System.out.println(a/b);
    }
}

在这里插入图片描述

出现异常,然后实现捕获异常

//Test
public class Test {
    public static void main(String[] args) {

        int a = 1;
        int b = 0;

        try {
            System.out.println(a/b);
        }catch (ArithmeticException e){
            System.out.println("程序出现异常,变量b不能为0");
        }finally {
            System.out.println("finally");
        }
    }
}

在这里插入图片描述

finally都会执行

手动栈溢出

//Test
public class Test {
    public static void main(String[] args) {

        try {   //监控区域
            new Test().a();
        }catch (Throwable e){ //捕获异常
            System.out.println("程序出现异常,捕获到Throwable");
        }finally {  //最后执行
            System.out.println("finally");
        }

    }
    public void a(){
        b();
    }
    public void b() {
        a();
    }
}

在这里插入图片描述

捕获 1/0 的异常等级

//Test
public class Test {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        try {   //监控区域
            System.out.println(a/b);
        } catch (Error e){ //捕获异常
            System.out.println("Error");
        } catch (Exception e){
            System.out.println("Exception");
        } catch (Throwable t){
            System.out.println("Throwable");
        } finally {  //最后执行
            System.out.println("finally");
        }  
    }
}

在这里插入图片描述

逐层递进关系 , 从小到大捕获

主动抛出异常

//Test
public class Test {
    public static void main(String[] args) {
        new Test().test(1,0);
    }
    public void test(int a, int b){
        if(b == 0){
            throw new ArithmeticException();//主动抛出异常
        }
    }
}

在这里插入图片描述


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值