day07 Java面向对象

面向对象

什么是面向对象

image-20240908164935438

方法的定义

package com.liu.oop;

import java.io.IOException;

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

    }
    /*
    修饰符 返回值类型 方法名(。。。){
        方法体
        return 返回值;
    }
     */
    public String sayHello() {
        return "Hello,world!";
    }
    public void hello(){
        return;
    }
    public int max(int a,int b){
        return a>b ? a:b;//三元运算符
        //a是否大于b,是打印a,否打印b
    }

    //数组下标越界:ArrayindexOutOfBoundsException
    public void raedFile(String file) throws IOException{

    }
}

方法的调用

静态方法/实参和形参

package com.liu.oop;

public class Demo03 {
    public static void main(String[] args) {
        //实际参数和形式参数的类型要对应!
        //调用静态方法
        int add = Demo03.add(1,2);
        System.out.println(add);
    }
    //静态方法
    public static int add(int a,int b){
        return a+b;
    }
}

非静态方法/实参和形参

package com.liu.oop;

public class Demo03_1 {
    public static void main(String[] args) {
        //实际参数和形式参数的类型要对应!
        //调用非静态方法
        int Demo03_1 = new Demo03_1().add(1,2);
        System.out.println(Demo03_1);
    }
    //非静态方法
    public int add(int a,int b){
        return a+b;
    }
}

值传递

package com.liu.oop;
//值传递
public class Demo04 {
    public static void main(String[] args) {
        int a=1;
        System.out.println(a);//1
        Demo04.change(a);
        System.out.println(a);//1
    }
    //返回值为空
    public static void change(int a){
        a=10;
    }
}

引用传递

package com.liu.oop;
//引用传递:对象,本质还是值传递
//对象 内存
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);//louis
    }
    public static void change(Person person) {
        //person是一个对象:指向的===》Person person = new Person();这是一个具体的人,可以改变属性!
        person.name="louis";
    }
}

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

类与对象

类与对象的创建

类与对象的关系

image-20240920122254737

image-20240920122303743

创建与初始化对象

image-20240920122559151

学生类
package com.liu.oop.demo02;
//学生类
public class Student {
    //属性:字段
    String name; //null(默认)
    int age; //0(默认)

    //方法
    public void study() {
        System.out.println(this.name+"在学习");
    }
}
主类(一个项目只能有一个main方法)
package com.liu.oop.demo02;

import com.liu.oop.demo02.Student;

//一个项目应该只存在一个main方法
public class Application {
    public static void main(String[] args) {
        //类:抽象的,实例化
        //类实例化后会返回一个自己的对象
        //student对象是一个Student类的具体实例!
        Student xiaoming = new Student();
        Student xiaohong = new Student();
        xiaoming.name = "小明";
        xiaoming.age = 18;
        System.out.println(xiaoming.name);//小明
        System.out.println(xiaoming.age);//18
        System.out.println(xiaohong.name);//null
        System.out.println(xiaohong.age);//0
    }
}

构造器详解

主类

package com.liu.oop.demo03;
import com.liu.oop.demo03.Person;
public class Application {
    public static void main(String[] args) {
        //new 实例化了一个对象
        Person personwc = new Person();
        Person personyc = new Person("Louis");
        System.out.println(personwc.name);//无参数构造器调用
        System.out.println(personyc.name);//有参数构造器调用
    }
}
/*
    构造器:
        1.和类名相同
        2.没有返回值,没有返回类型
     作用:
        1.new 本质在调用构造方法
        2.初始化对象的值
    注意点:
        1.定义有参构造之后,如果想使用无参构造,必须显示定义无参构造
 */

Person类

package com.liu.oop.demo03;

public class Person {
    //一个类即使什么都不写,它也会存在一个方法
    //显示的定义构造器
    String name;
    //1.使用new关键字,本质是在调用构造器
    //2.用来初始值
    public Person(){
        this.name = "Louis";
    }
    //有参构造:一旦定义了有参构造,无参就必须显示定义
    public Person(String name){
        this.name = name;
        //this.**是代表当前类的name
        //后面name一般是参数传进来的值
    }
    //alt+insert:快速生成构造器
    //笔记本如果按不出来尝试:Fn+alt+insert
}

创建对象内存分析

Pet类

package com.liu.oop.demo04;

public class Pet {
    public String name;
    public int age;
    //无参构造
    public void shout(){
        System.out.println(name+"叫了一声");
    }
}

主类

package com.liu.oop.demo04;
import com.liu.oop.demo04.Pet;

public class Application {
    public static void main(String[] args) {
        Pet dog = new Pet();
        dog.name = "小黑";
        dog.age = 3;
        dog.shout();
        System.out.println(dog.age);
        System.out.println(dog.name);
        System.out.println("===============");
        Pet cat = new Pet();
        dog.name = "小白";
        dog.age = 3;
        dog.shout();
        System.out.println(dog.age);
        System.out.println(dog.name);
    }
}

简单小结

1.类与对象
    类是一个模板:抽象,对象是一个具体的实例
2.方法
    定义、调用!
3.对应的引用
    引用类型:基本类型(8)
    对象是通过引用来操作的:栈====》堆
4.属性:字段field 成员变量
    默认初始化:
    	数字:0 0.0
    	char:u0000
    	booleanfalse
    	引用:null
	修饰符 属性类型 属性名 = 属性值
5.方法
6.对象的创建和使用
    - 必须使用new 关键字创建对象, 构造器 Person Louis = new Person();
	- 对象的属性 Louis.name
    - 对象的方法 Louis.sleep()
7.类
        静态的属性   属性
        动态的行为   方法

封装、继承、多态

封装

Student类

package com.liu.oop.demo05;
//类 private:私有 public:公开
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 笔记本 Fn+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 = 10086;
        }else {
            this.age = age;//年龄合法
        }
    }
}

主类

package com.liu.oop.demo05;

import static sun.security.pkcs11.wrapper.Functions.getId;

public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student();
        s1.setName("louis");
        s1.setId(1);
        s1.setSex('男');
        s1.setAge(18);
        s2.setAge(79789);//年龄不合法
        System.out.println(s1.getName());
        System.out.println(s1.getId());
        System.out.println(s1.getSex());
        System.out.println("s1age:"+s1.getAge());//18
        System.out.println("s2age:"+s2.getAge());//打印出10086
    }
}
/*
   ==封装的意义==
1. 提高程序的安全性,保护数据
2. 隐藏代码的实现细节
3. 统一接口
4. 系统可维护性提高了
*/

继承

image-20240921130738367

Java中类只有单继承,没有多继承!
一个子类只能有一个父类,但是一个父类可以有多个子类

继承基础认识

主类
package com.liu.oop.demo6;

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.say();
        System.out.println(student.money);
    }
}
Person类
package com.liu.oop.demo6;
//在Java中,所有的类,都默认直接或者间接继承Object类
//Person : 人:父类
public class Person /*extends Object*/{
    //public 公共的,可继承(优先级最高)
    //protected 受保护的,可继承
    //default 默认的,可继承
    //private 私有的,不可继承
    public void say(){
        System.out.println("说了一句话");
    }
    public int money = 10_0000_0000;
}
学生类
package com.liu.oop.demo6;
//学生 is 人 :派生类、子类
//子类继承了父类,就会拥有父类的全部方法!
public class Student extends Person{
    //ctrl + h 查看继承关系
}
老师类
package com.liu.oop.demo6;
//老师 is 人 :派生类、子类
public class Teacher extends Person{
}

super - this

super注意点:
    1.super调用父类的构造方法,必须在构造方法的第一个
    2.super必须只能在子类的方法或者构造方法中!
    3.super和this不能同时调用构造方法!
 VS this:
    代表的对象不同:
        this:本身调用者这个对象
        super: 代表父类对象的应用
    前提
        this: 没有继承也可以使用
        super: 只能在继承条件才可以使用
    构造方法
        this():本类的构造
        super():父类的构造
主类
package com.liu.oop.demo07;

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

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.test1("清风");
        student.test2();

    }
}
Person类
package com.liu.oop.demo07;
public class Person {
    public Person() {
        System.out.println("Person无参执行了");
    }

    protected String name = "louis";
    public void print(){
        System.out.println("Person");
    }
}
Student主类
package com.liu.oop.demo07;

import com.liu.oop.demo07.Person;
public class Student extends Person {
    public Student() {
        //隐藏代码:默认调用了父类的无参构造
        super();//调用父类的构造器,必须要在子类的第一行(隐藏代码)
        //this();//跟super不能同时调用
        System.out.println("Student无参执行了");
    }

    private String name = "qf";
    public void print(){
        System.out.println("Student");
    }
    public void test1(String name){
        System.out.println(name);//清风
        System.out.println(this.name);//qf,当前
        System.out.println(super.name);//louis,父类
    }
    public void test2(){
        print();//Student
        this.print();//Student
        super.print();//Person
    }
}

方法重写

static关键字(拓展)
在Java中,方法是否加上static关键字有着明显的区别,主要体现在以下几个方面:
1.访问方式:
	静态方法(加static)可以直接通过类名来访问,不需要创建类的实例。
	非静态方法(不加static)需要先创建类的一个实例,然后通过这个实例来访问。
2.可访问的变量和方法:
	静态方法只能访问静态变量和静态方法,因为它不依赖于任何特定的对象实例。
	非静态方法可以访问实例变量和实例方法,也可以访问静态变量和静态方法。
3.内存中的存在形式:
	静态方法随着类的加载而加载到内存中,并且在整个应用程序运行期间都存在于内存中,直到JVM关闭。
	非静态方法随着对象的创建而加载到内存中,并且当对象被垃圾回收时,这些方法也会被销毁。
4.多线程中的锁定行为:
	如果一个方法没有加static,那么同步操作(如synchronized)锁住的是调用该方法的对象实例。
	如果一个方法加了static,那么同步操作锁住的是类的Class对象,即对于所有的实例来说都是同一个锁。
5.使用this和super:
	静态方法中不能使用this或super关键字,因为它们是针对具体实例的引用。
	非静态方法可以使用this和super关键字来引用当前对象实例和父类。
6.初始化时机:
	类的静态部分(包括静态方法和静态变量)会在类首次被加载时初始化。
	类的非静态部分(包括非静态方法和非静态变量)会在创建类的第一个实例时初始化。
总结来说,static关键字决定了方法是否属于类本身而不是类的实例。选择使用静态方法还是非静态方法取决于具体的应用场景和设计需求。
重载出现的指示(原为静态方法,重载的是非静态方法)

image-20240921152805205

方法重写(alt +inster 笔记本:Fn+alt+inster ===>Override)

A类
package com.liu.oop.demo08;
//继承
public class A extends B{
    //Override:重写
    @Override //注解:有功能的注释
    public void test() {
        System.out.println("A===>test");
    }
}
B类
package com.liu.oop.demo08;
//重写都是方法的重写与属性无关
public class B {
    public void  test(){
        System.out.println("B===>test");
    }
}
主类
package com.liu.oop.demo08;

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() 非静态:A===>test()
    }
}
方法重写小结
重写:需要有继承关系,子类重写父类的方法!
    1.方法名必须相同
    2.参数列表列表必须相同
    3.修饰符:范围可以扩大但不能缩小:public>protected>default>private
    4.抛出的异常:范围可以被缩小但不能扩大:EXception(大)>>ClassNotfoundException
重写,子类的方法和父类必要一致,方法体不同!

为什么需要重写:
    1.父类的功能,子类不一定需要,或者不一定满足!
    Alt+Insert: Override

多态

image-20240921160936578

Person类

package com.liu.oop.demo09;

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

Student类

package com.liu.oop.demo09;

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

主类

package com.liu.oop.demo09;

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();//原为run,现在为son,run:子类重写了父类的方法,执行子类的方法
        s1.run();
        //s2.eat();Person类没有eat方法,不能调用
        ((Student) s2).eat();//强制转换高==>低,Person==>Student
    }
}

多态注意事项

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

instanceof关键字用于检查:

·一个对象是否属于特定类或其子类。
·返回值为布尔类型,如果是则返回true,否则返回false。
主类
package com.liu.oop.demo10;

public class Application1 {

    public static void main(String[] args) {
        //Object > String
        //Object > Person > Student
        //Object > Person > Teacher
        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);//能不能编译通过
    }
}
Person类
package com.liu.oop.demo10;

public class Person {
    public void run(){
        System.out.println("run");
    }
}
Student类
package com.liu.oop.demo10;

import com.liu.oop.demo10.Person;

public class Student extends Person {
    public void go(){
        System.out.println("go");
    }
}
Teacher类
package com.liu.oop.demo10;

public class Teacher extends Person{
}

类型转换(其他类同instanceof内容)

1.父类引用指向子类的对象
2.把子类转换为父类,向上转型
3.把父类转换为子类,向下转型  (强制转型)
4.方便方法的调用,减少重复的代码! 简洁
package com.liu.oop.demo10;

public class Application2 {
    public static void main(String[] args) {
        //类型之间的转化: 父(高)    子(低)
        //高                   低
        Person student = new Student();
        //student将对象转换为Student类型,我们就可以调用Student中的方法
        Student s = (Student)student;
         s.go();
         //或
        ((Student)student).go();
        //子类转换成父类可能会丢失自己的本来的一些方法
        Student student1 = new Student();
        student1.go();
        Person person = student1;
        //person.go();无法直接调用了
    }
}

static关键字详解

主类

package com.liu.oop.demo11;

public class Application {
}

Person类

package com.liu.oop.demo11;

public final class Person { //final修饰的类不能被继承,就没有子类了
    //2:赋初始值
    {
        //代码块(匿名代码块)
        System.out.println("Person的匿名代码块");
    }
    //1:静态代码块只执行一次
    static {
        //静态代码块
        System.out.println("Person的静态代码块");
    }
    //3
    public Person(){
        System.out.println("Person的构造方法");
    }

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

Student类

package com.liu.oop.demo11;
//static
public class Student /*extends Person*/{
    private static int age;//静态的变量  多线程!
    private  double score;//非静态变量
    public static void go() {}
    public void run() {
        go();//非静态方法可以调用静态方法
    }
    public static void main(String[] args) {
        Student s1 = new Student();
        System.out.println(Student.age);
        System.out.println(s1.age);
        //System.out.println(Student.score);非静态的字段不可这样调用
        System.out.println(s1.score);

        //静态方法不能直接调用非静态方法
        //非静态方法可以调用静态方法
        //静态方法可以调用静态方法
        //run();
        s1.run();//需要通过对象调用
        go();
    }
}

Test类

package com.liu.oop.demo11;
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test {
    public static void main(String[] args) {
        System.out.println(Math.random());
        System.out.println(random());//静态导入包,可以直接调用方法
        System.out.println(PI);//静态导入包,可以直接调用方法
    }
}

抽象类

image-20240922135752926

主类

package com.liu.oop.demo12;

public class Application {
}

A类

package com.liu.oop.demo12;
//抽象类的所有方法,继承了它的子类,都必须要实现它的方法~ 除非子类也是抽象类
public class A extends Action{
    @Override
    public void doSomething() {

    }
}

Action类

package com.liu.oop.demo12;
//abstract 抽象类:类extends:单继承~ (接口可以多继承)
public abstract class Action {
    //约束~有人帮我们实现~
    //抽象方法,只有方法的名字,没有方法的实现
    public abstract void doSomething();
    //1.不能new这个抽象类,只能靠子类去实现它: 约束!
    //2.抽象类可以写普通的方法
    //3.抽象方法必须在抽象类中
    //抽象的抽象:约束!
}

接口

接口的作用

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

UserServices接口

package com.liu.oop.demo13;
//interface 定义的关键字 ,接口都需要有实现类
public interface UserServices {
    //接口中的所有定义的方法其实都是抽象的 public abstract
    public abstract void run();
    void  eat();
    void sleep(String something);
    //常量
    int AGE = 99;
    public static final int DAY = 99;
}

TimeServices接口

package com.liu.oop.demo13;

public interface TimeServices {
    void timer();
}

UserServicesImpl类

package com.liu.oop.demo13;
//抽象类 : extends
//类 可以实现接口implements接口
//实现了接口的类,必须重写接口中的所有方法
//多继承: 利用接口实现伪多继承
public class UserServicesImpl  implements UserServices,TimeServices{
    //重写接口中的方法
    @Override
    public void eat() {
        System.out.println("吃东西");
    }

    @Override
    public void run() {
        System.out.println("跑");
    }

    @Override
    public void sleep(String something) {
        System.out.println("睡"+something);
    }

    @Override
    public void timer() {

    }
}

内部类

image-20240922143014997

Outer类

package com.liu.oop.demo14;

public class Outer {
    private int id=10;
    public void out(){
        System.out.println("这是外部类的方法");
    }
    public void method(){
        //局部内部类
        class Inner{
            public void in(){
                System.out.println("这是局部内部类的方法");
            }
        }
    }
    public class Inner{
        public void in(){
            System.out.println("这是成员内部类的方法");
        }
        //获得外部类的私有属性
        public void getID(){
            System.out.println(id);
        }
    }
    public static class StaticInner{
        public void in(){
            System.out.println("这是静态内部类的方法");
        }
        /*
        //不能获得外部类的私有属性,除非是外部类也是静态的
        public void getID(){
            System.out.println(id);
        }
        */
    }
}
//一个类中可以有多个class类,但是只能有一个public class类、
class A{
    void show(){
        System.out.println("也是一个内部类");
    }
}
interface B{
    public void hello();
}

主类

package com.liu.oop.demo14;

public class Application {
    public static void main(String[] args) {
        Outer outer = new Outer();
        // 通过外部类来实例化内部类
        Outer.Inner inner = outer.new Inner();
        inner.getID();
        //没有名字初始化类,不用将实例保存到变量中
        new A().show();//匿名内部类
        new B(){
            @Override
            public void hello(){
                System.out.println("这是匿名内部类的方法");
            }
        };
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值