Java面向对象基础


一、面向对象编程概念

1、面向对象思想

在这里插入图片描述

2、什么是面向对象?

本质:以类的方式组织代码。以对象组织封装数据。
在这里插入图片描述

3、方法回顾和加深

在这里插入图片描述
1、方法定义回顾:

package com.oop.Demo01;

import java.io.IOException;

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

    }

    // String:返回一个字符串
    public static String sayHello(){
        return "hello,world";// return代表方法结束,返回一个结果
    }

    //void:返回一个空值
    public void print(){
        return;
    }

    //int:返回一个整数
    public int max(int a,int b){
        return a>b ? a :b;//三元运算符
    }
    //读文件  抛出异常
    public void readFile(String file)throws IOException {

    }
}

2、静态方法和非静态方法的调用区别:

package com.oop.Demo01;

public class Demo02 {

    public static void main(String[] args) {
        // 静态方法 static
        Student.say();

        // 非静态方法
        ///实例化这个类 new
        //对象类型  对象名  =  对象值
        Student student = new Student();
        student.say();
    }

    // static 和类一起加载的
    public static void a(){
        b();
    }

    //类实例化才存在
    public static void b(){

    }
}

package com.oop.Demo01;

//学生类
public class Student {

    //非静态方法
    public static void say(){
        System.out.println("学生说话了");
    }

    public void hello(){
        System.out.println("你好啊");
    }
}

3、static方法不能调用不是静态方法的方法:

package com.oop.demo01;

public class Demo02 {

    public static void main(String[] args) {}
    // static 和类一起加载
    public static void a(){
        b();
        // 报错 Non-static method 'b()' cannot be referenced from a static context
    }
    // 类实例化之后才存在
    public void b(){}
}

4、形参和实参

package com.oop.Demo01;

public class Demo03 {
    public static void main(String[] args) {
        //不加 static
//        int add = new Demo03().add(1, 2);
//        System.out.println(add);

        //       实际参数要与形式参数的类型要对应
        int sum = Demo03.add(1, 2);
        System.out.println(sum);

    }

    public  static int add(int a,int b){
        return a+b;
    }
}

5、java都是值传递

package com.oop.Demo01;

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

        Demo04.change(a);
        System.out.println(a);//1
    }

    //返回值为空
    public static void change(int a){
        a = 10;
    }
}

6、引用传递

package com.oop.Demo01;

//引用传递:对象,本质还是值传递
public class Demo05 {
    public static void main(String[] args) {
        Person person = new Person();

        System.out.println(person.name);//null

        Demo05.change(person);

        System.out.println(person.name);//李白

    }
    public static void change(Person person){
        //person是一个对象:指向的 ---> Person person = new Person(); 这是一个具体的人,可以改变属性
        person.name = "李白";
    }

}
//定义了一个Person类,有一个属性:name
class Person{
    String name;//null
}

二、类与对象

1、类与对象的关系

在这里插入图片描述

2、创建与初始化对象

在这里插入图片描述

package com.oop.Demo02;

//一个项目应该只存一个main方法
public class Application {
     public static void main(String[] args) {

        //类:抽象的,实例化
        //类实例化后会返回一个自己的对象
        //student对象就是一个Student类的具体实例
        Student xiaoming = new Student();
        Student xh = new Student();

        xiaoming.name = "小明";
        xiaoming.age = 3;

        xh.name = "小红";
        xh.age = 3;

        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);

        System.out.println(xh.name);
        System.out.println(xh.age);
    }

}

package com.oop.Demo02;

//学生类
public class Student {

    //属性:字段
    String name;//null
    int age;//0

    //方法
    public void study(){
        System.out.println(this.name+"在学习");
    }

}

3、构造器

  • 一个类即使什么都不写,它会存在一个方法。
package com.oop.Demo02;

public class Person {

    public Person() {
    }
}

  • 作用1:使用new关键字,本质就在调用构造器。

  • 作用2:初始化值

    测试:

package com.oop.Demo02;

//一个项目只存在一个main方法
public class Application {
    public static void main(String[] args) {

        //实例化一个对象
        Person person = new Person();
        System.out.println(person.name);//libai
    }
}

package com.oop.demo02;

public class Person {
    String name;
    //一个类即使什么都不写,它会存在一个方法
    //显示的定义构造器

    //实例化初始值
    //1、使用new关键字,本质就在调用构造器
    //2、初始化值
    public Person(){
        this.name = "libai";
    }

}

  • 有参构造: 一旦定义了有参构造,无参构造就必须显示定义
    在这里插入图片描述
  • 快捷键:Alt+Insert自动生成构造器

构造器总结:
1、和类名相同
2、没有返回值

作用:
1、使用new关键字,本质就在调用构造器
2、初始化值

注意点:定义有参构造之后,如果想使用无参构造,无参构造就必须显示定义
this. = 传进来的值;

package com.oop.Demo02;

public class Person {

    //一个类即使什么都不写,他也会存在一个方法
    //显示的定义构造器

    String name;
    int age;

    //实例化初始值
    //1.使用new关键字,本质是在调用构造器
    //2.用来初始化值
    public Person(){

    }

    //有参构造:一旦定义了有参构造,无参就必须显示定义
    public Person(String name){
        this.name = name;
    }

    //alt + insert

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}





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

        //new 实例化一个对象
        Person person = new Person("libai",23);

        System.out.println(person.name);
    }


    构造器:
        1.和类名相同
        2.没有返回值
    作用:
        1.new本质在调用构造方法
        2.初始化对象的值
    注意点:
        1.定义有参构造之后,如果想使用无参构造,显示的定义一个无参的构造
    alt+insert

    this. =当前对象传进来的值;
 */

4、内存图示

在这里插入图片描述
在这里插入图片描述

5、总结

在这里插入图片描述

三、面向对象三大特性

1、封装

在这里插入图片描述
意义:

​ 1、提高程序的安全性

​ 2、隐藏代码的实现细节

​ 3、统一接口

​ 4、系统可维护性增加了

package com.oop.demo04;

//类 private:私有
public class Student {

    //属性私有
    private String name;    //名字
    private int id;    //学号
    private char sex;    //性别
    private int age;

    //提供一些可以操作这个属性的方法!
    //提供一些public的get、set方法

    //get获得这个数据
    public  String getName(){
        return this.name;
    }
    //set给这个数据设置值
    public void setName(String name){
        this.name = name;
    }

    //alt + insert
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age>120 || age<0){
            this.age = 3;
             System.out.println("年龄不合法");
        }else {
            this.age = age;
        }

    }
}

测试:

package com.oop;

import com.oop.demo04.Student;
/*
1.提高程序的安全性,保护数据
2.隐藏代码的实现细节
3.统一接口
4.系统可维护性增加了
 */

//一个项目应该只存一个main方法
public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();

        s1.setName("李白");

        //方法名,参数列表
        System.out.println(s1.getName());

        s1.setAge(999);//不合法的
        System.out.println(s1.getAge());
    }

}

2、继承

在这里插入图片描述
子类继承了父类,就会拥有父类的全部方法。

在java中,所有的类都默认直接或间接的继承object类

Person父类:

package com.oop.demo04;

//父类
public class Person /*extends Object*/ {
    public int money1  = 10_0000_0000;
    private int money2  = 20_0000_0000;

    public void say(){
        System.out.println("hello");
    }

    public int getMoney2() {
        return money2;
    }

    public void setMoney2(int money2) {
        this.money2 = money2;
    }
}

Student子类:

package com.oop.demo04;

//子类 ---学生 is 人
//子类继承了父类,就会拥有父类的全部方法。
public class Student extends Person {

    //快捷键: Ctrl + H
}

Teacher子类:

package com.oop.demo04;

//子类 ---老师 is 人
public class Teacher extends Person{
}

测试:

package com.oop;

import com.oop.demo04.Student;

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.say(); //hello
        System.out.println(student.money1); //10_0000_0000
        //System.out.println(student.money2);//父类 private 不能继承
    }
}

1、super、this

在这里插入图片描述
私有的东西无法被继承!

super注意点:
1.super调用父类的构造方法,必须在构造方法的第一个
2.super必须只能出现在子类的方法或者构造方法中!
3.super 和 this 不能同时调用构造方法

和this的对比:

  • 代表的对象不同:

     this:本身调用者这个对象
     super:代表父类对象的应用
    
  • 前提:

     this:没有继承也可以使用
     super:只能在继承条件才可以使用
    
  • 构造方法

     this();本类的构造
     super();父类的构造
    

父类:

package com.oop.demo05;

//父类
public class Person /*extends Object*/ {

    public Person() {
        System.out.println("Person无参构造执行了");
    }

    protected String name = "父类的名字";

    public void print(){
        System.out.println("Person");
    }
}


子类:

package com.oop.demo05;

//子类 ---学生 is 人
//子类继承了父类,就会拥有父类的全部方法。
public class Student extends Person {
    //无参
    public Student() {
        //隐藏代码,调用了父类的构造器
        super(); //调用父类的构造器,必须在子类构造器的第一行
        System.out.println("Student无参构造执行了");
    }

    //有参
    public Student(String name) {
        this.name = name;
    }

    private String name = "子类的名字";

    public void print(){
        System.out.println("Student");
    }

    public void test1(){
        print();
        this.print();
        super.print();

    }

    public void test(String name){
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);

    }
}


测试:

package com.oop;

import com.oop.demo05.Student;

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        /*
            Person无参构造执行了
            Student无参构造执行了
         */
        student.test("传递的名字");
        /*
            传递的名字
            子类的名字
            父类的名字
         */
        student.test1();
        /*
            Student
            Student
            Person
         */
    }
}


2、方法重写

重写都是方法的重写,和属性无关

父类的引用指向子类是可以的

方法的调用只和左边的相关,即定义的数据类型有关

package com.oop.demo05;
//重写都是方法的重写,和属性无关
public class B {
    public static void test(){
        System.out.println("B->test");
    }
}

package com.oop.demo05;

//重写都是方法的重写,和属性无关
public class A extends B {
    public static void test(){
        System.out.println("A->test");
    }
}

测试:

package com.oop;

import com.oop.demo05.A;
import com.oop.demo05.B;

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

        //方法的调用只和左边的相关
        A a = new A();
        a.test(); //A->test

        //父类的引用指向子类是可以的
        B b = new A();
        b.test(); //B->test  类的优先级比对象大
    }
}

快捷键:Alt + Insert—>Override Method

静态的方法和非静态的方法区别很大

package com.oop.demo05;
//重写都是方法的重写,和属性无关
public class B {
    public  void test(){
        System.out.println("B->test");
    }
}

package com.oop.demo05;

//重写都是方法的重写,和属性无关
public class A extends B {
    @Override//重写 有功能的注释
    public void test() {
        System.out.println("A->test");
    }
}

测试:

package com.oop;

import com.oop.demo05.A;
import com.oop.demo05.B;

public class Application {
    //静态的方法和非静态的方法区别很大
    public static void main(String[] args) {

        // 方法的调用只和左边的相关
        A a = new A();
        a.test(); //A->test

        // 父类的引用指向子类是可以的
        B b = new A();// 子类重写了父类的方法
        b.test(); //A->test
    }
}

3、重写总结:

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

  1. 方法名必许相同
  2. 参数列表必须相同
  3. 修饰符:范围可以扩大但是不能缩小(Public->Protect->Default->Private)
  4. 抛出的异常:范围可以缩小但是不能扩大;ClassNotFoundException --> Exception(大)

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

为什么需要重写?

  1. 父类的功能不一定需要
  2. 父类的功能不一定满足

3、多态

在这里插入图片描述

在这里插入图片描述

package com.oop.demo06;

public class Person {
    public void run(){
        System.out.println("run");
    }
}
/*
多态注意事项:
    1.多态是方法的多态,属性没有多态
    2.父类和子类,有联系 类型转换异常 ClassCastException!
    3.存在条件:继承关系,方法需要重写,父类的引用指向子类对象 father f1 = new Son();

    不能重写:
        1.static静态方法,属于类,它不属于实例
        2.final 常量;
        3.private方法;
 */
package com.oop.demo06;

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

    public void eat(){
        System.out.println("eat");
    }
}

测试:

package com.oop;

import com.oop.demo06.Person;
import com.oop.demo06.Student;

//一个项目应该只存一个main方法
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();

        //对象能执行哪些方法,主要看对象左边的类型,和右边的关系不大
        ((Student) s2).eat();//子类重写了父类的方法,执行子类的方法
        s1.run();
    }

}

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

不能重写:
1.static静态方法,属于类,它不属于实例
2.final 常量;
3.private方法;

instanceof

X instanceof Y:用X和Y是否是父子关系来判断是否能编译通过。

package com.oop.demo07;

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

package com.oop.demo07;

public class Student extends Person{
  
}

package com.oop.demo07;

public class Teacher extends Person{
}

package com.oop;

import com.oop.demo07.Person;
import com.oop.demo07.Student;
import com.oop.demo07.Teacher;

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); //true
        System.out.println(object instanceof Object); //true
        System.out.println(object instanceof Teacher); //false
        System.out.println(object instanceof String); //false

        Person person = new Student();
        System.out.println("============================");
        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); //编译报错

        Student student = new Student();
        System.out.println("============================");
        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); //编译报错
    }
}

总结

一个对象的实际类型是确定的,可以指向的引用类型就不确定了
//new Student(); 确定的
//new Person(); 确定的

  1. 对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
  2. 子类能够调用的方法只有自己的或者继承的
  3. 父类可以指向子类,但是不能调用子类独有的方法
  4. 父类/接口 变量名 = new 子类/实现类();
  5. Person s2 = newStudent(); //父类的引用指向子类
  6. 多态:父类的变量指向子类对象

条件:

  1. 父类引用指向子类的对象
  2. 把子类转换为父类,向上转型,会丢失一些方法
  3. 把父类转换为子类,向下转型,需要强制转换
  4. 方便方法的调用,减少重复的代码。

static

1、static变量、方法

1.static 跟类一起产生
2.静态方法不能调用非静态方法,非静态方法可以调用静态方法
3.类可以直接调用静态static变量和static方法
4.类的对象可以调用static和非static方法和变量

package com.oop.demo08;

// static   跟类一起产生
public class Student {
    
    private static int age; //静态变量
    private double score; //非静态的变量
    
    //静态方法
    public static void run(){
        System.out.println("run");
        // go();  静态方法不能调用非静态方法
    }
    
    //非静态方法
    public void go(){
        System.out.println("go");
        run(); //非静态方法可以调用静态方法
    }

    
    public static void main(String[] args) {
        Student s1 = new Student();
        System.out.println(s1.age);
        System.out.println(s1.score);
        System.out.println(Student.age); //类直接调用
        //System.out.println(Student.score); //false

        s1.run();
        s1.go();
        Student.run();
        //Student.go;  false类不能直接调用

        run();
        //go(); //false

    }
}

2、static代码块

  1. 静态代码块:跟类一起产生,永久只执行一次
  2. 匿名代码块跟对象同时产生
  3. 先执行静态代码块,再执行匿名代码块,再执行构造方法
package com.oop.demo08;

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 person1 = new Person();
        System.out.println("=============");
        Person person2 = new Person();
    }
}

//运行结果:
/*
静态代码快
匿名代码快
构造方法
=============
匿名代码快
构造方法
 */

3、静态导入包

package com.oop.demo08;

//静态导入包
import static java.lang.Math.PI;
import static java.lang.Math.random;

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

五、抽象类和接口

抽象类

在这里插入图片描述

总结1

  1. abstract抽象方法,只有方法名字,没有方法的实现;有人帮我们实现。
  2. 抽象类的所有方法,继承了他的子类,都必须要实现它得方法。
  3. 继承了抽象类的子类,必须实现父类的所有抽象方法。
  4. 类只能单继承,但是接口相当于多继承(接口多实现)。

总结2

  1. 不能new抽象类,只能靠子类去实现它:约束
  2. 抽象类里面可以有普通方法
  3. 抽象方法必须在抽象类中
  4. 抽象的抽象:约束~
  5. 存在构造器么?存在的意义?提高开发效率

实例

package com.oop.demo09;

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

    //约束~ 有人帮我们实现
    //abstract:抽象方法,只有方法名字,没有方法的实现!
    public abstract void doSomething();
}

package com.oop.demo09;

//抽象类的所有方法,继承了他的子类,都必须要实现它得方法
public class A extends Action{
    @Override
    public void doSomething() {
    }
}

接口

~~在这里插入图片描述
接口自己无法写方法,是专业的约束!约束和实现分离:面向接口编程

作用

  1. 接口是一个约束
  2. 定义一些方法,让不同的人实现
  3. 接口中的方法:public abstract
  4. 接口中的常量:public static final
  5. 接口不能被实例化~接口中没有构造方法
  6. implements可以实现多个接口
  7. 必须要实现接口中的方法

实例

接口1:

package com.oop.demo10;

// interface定义的关键字,接口都需要有实现类

public interface UserService {

    //常量(全局性的)

    public static final int AGE1 = 99;
    //相当于
    int AGE2 = 99;

    //接口中的所有定义都是抽象的public

    public abstract void add(String name);
    //public abstract可以省略
    public abstract void delete(String name);
    public abstract void update(String name);
    public abstract void query(String name);

}

接口2

package com.oop.demo10;

public interface TimeService {
    void timer();
}

测试

package com.oop.demo10;

// 一个类可以实现接口 implements: 接口
// 实现了接口的类,就需要实现接口中的方法~

//利用接口可以实现伪多继承
public class UserServiceImp1 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() {

    }
}

六、内部类

在这里插入图片描述
一个java类中可以有多个class类,但是只能有一个public class类

package com.oop.demo11;

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);
        }
    }

    //一个java类中可以有多个class类,但是只能有一个public class类
    class A{
        public  void main(String[] args) {
            System.out.println();
        }
    }

    //局部内部类
    public void method(){
        class Inner{
          public void in(){

          }
        }
    }
}

test:

package com.oop;

import com.oop.demo11.Outer;


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

        //new 外部类
        Outer outer = new Outer();
        outer.out();

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

package com.oop.demo11;

public class Test {
    public static void main(String[] args) {
        
        // 没有名字初始化类,不用将实例保存到变量中
        new Apple().eat();
        
        // 必须重写
        UserService userService = new UserService(){
            @Override
            public void hello() {

            }
        };
    }
}

class Apple{
    public void eat(){
        System.out.println("eat");
    }
}

//接口:
interface UserService{
    void hello();
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值