java面向对象基础知识点

面向对象

一、什么是面向对象

面向过程:微观

面向对象:宏观、分类思想 本质为以类的方式组织代码、以对象的组织封装数据。

例如:人类里面分好几种:比如警察、学生、老师等对象。

二、对象的创建

1.方法补充

public void test() {
    return; //这里可以不写,如果要写一个return就可以
}
public static void a() {  //和类一起加载的
        b();//报错
}
public void b() { //类实例化之后才存在
        
}

public void b //必须得实例化对象才调用

2.对象创建

  • 代码分屏:split vertically

一个类里面只有属性和方法

值传递:

public class Demo05 {
    //值传递
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a);//1
        Demo05.change(a);
        System.out.println(a);//1
    }
    public static void change(int a) {
        a = 10;
    }
}

引用传递:指向真实内存的地址值。

//引用传递
public class Demo06 {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);
        change(person);
        System.out.println(person.name);
    }
    public static void change(Person person) {
        person.name = "cs";
    }
}
class Person{
    String name;
}

3.构造器

一个类什么都不写,也会存在一个构造器

1.进行创建对象时候调用

2.和类名相同

3.没返回类型,不能写void。

快捷键:alt+insert

image-20210731121914854

三、面向对象三大特征

1.封装

属性私有,get/set,提高代码安全性,隐藏细节,统一接口,系统可维护增加

image-20210731122510805

2.继承

extands,只有单继承

子类继承父类,就会拥有父类全部方法,私有的东西无法继承

ctrl+H查看继承树

所有类,默认直接间接继承object

super调用父类

调用父类构造器,必须在子类构造器的第一行

  • super和this对比:

this没继承能使用,this()本类的构造

super继承才能使用,super()父类的构造

  • 方法重写:

image-20210731125104398

3.多态

同一种方法可以根据发送对象不同采取不同行为方式

是方法多态,属性没有多态

Student s1 = new Student
Person s2 = new Student
Object s3 = new Student

instanceof:判断两个类是否父子关系

public class Application {
    public static void main(String[] args) {
        //Object>Person>Student
        //Object>Person>Teacher
        //Object>String
       Object object = new Student();
        System.out.println(object instanceof Student);//true
        System.out.println(object instanceof Person);//ture
        System.out.println(object instanceof Object);//true
        System.out.println(object instanceof Teacher);//False
        System.out.println(object instanceof String);//False
    }
}
  • 类型转换:子类转换为父类,会丢失自己本来的一些方法

    public static void main(String[] args) {
            //类型转换 父子
            //高              低    低转高自动转换
            Person student = new Student();
            //将这个对象转换为Student类型
    
            Student student1 = (Student) student;
            student1.go();
            // 写成一句话
            ((Student)student).go();
        }
    }
    

四、抽象类和接口类

抽象方法

抽象方法,只有方法名字,没有方法的实现。

abstract可修饰方法也可修饰类

抽象类里可以没有抽象方法,但是有抽象方法一定要抽象类

抽象类不能使用new关键字来创建对象,用来让子类继承的

接口类

本质是契约

public interface UserService {
    //接口中所有定义是抽象的public abstract  ,接口需要有实现类。

    //常量 public static final
    //int AGE = 99;一般不会在接口里定义常量
    void run(String name);
}
  


//一个类可以实现接口,用implements实现   要重写方法
//可利用接口多继承
public class UserServiceImpl implements UserServise {

    @Override
    public void run(String name) {
    }
}

五、内部类

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



public class Application {
    public static void main(String[] args) {
        Outer outer = new Outer();

        //通过外部类来实例化内部类
        Outer.Inner inner = outer.new Inner();
        inner.in();

    }
}

六、打开class文件

image-20210731120553549

image-20210731120810032

image-20210731120853006

七、static使用

  • 非静态方法可以直接调用静态方法,main方法也是static

原理:非静态方法加载比静态方法慢

  • 匿名代码块

    {
    	//匿名代码块
    }
    
    static {
    	//静态匿名代码块
    }
    
    public Person {
    	//构造方法	
    }
    
    先输出静态代码块-----匿名-----构造
    

八、异常

代码

image-20210731215112047

try catch finally throw throws

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){

        }finally {
            System.out.println("finally");//可以不要finally
        }
    }
}

主动抛出异常:

if(b==0){
    throw new ArithmeticException();//一般在方法中使用
}

快捷键:

image-20210731220420291

参考自狂神说b站视频

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值