Java~对象

本文详细讲解了Java中类与对象的关系,包括构造器的使用、属性与方法、继承与重写、封装概念、多态实例以及抽象类和接口。通过实例演示,探讨了如何创建对象、访问私有属性和方法,以及接口和抽象类在设计模式中的角色。
摘要由CSDN通过智能技术生成

类与对象的关系

Person类,Pet类,Car类

这些类是用来描述/定义某一些具体的事物应该具备的特征和行为

构造器

构造器:

  1. 和类名相同
  2. 没有返回值

作用

  1. new 本质在调用结构方法
  2. 初始化对象的值

注意点

  1. 定义有参结构之后,如果想使用无参结构,显示的定义一个无参的结构

快捷键:Alt+ insert

this . (什么是表示当前类的) = (什么值);

先生成一个main方法,其他的都用这个调用输出即可

package com.oop.dome02;
//一个项目应该只存在一个main方法
public class Application {
    public static void main(String[] args) {
     
    }
}

Student的类

package com.oop.dome02;
//学生类
public class Student {

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

    //方法
    public  void study(){
        System.out.println(this.name+"在学习");//this表示引用当前的类
    }


}
/**
 * public static void main(String[] args) {
 *         //类:抽象的 要 实例化
 *         //实例化后就会返回一个自己的对象!
 *         //student对象就是一个Student类的具体实例!
 *         //Student student = new Student();
 *         Student xiaoming = new Student();
 *         Student xiaohong = new Student();
 *
 *
 *         xiaoming.name = "小明";//如果没有给值输出的是null
 *         xiaoming.age = 5;
 *
 *         System.out.println(xiaoming.name);
 *         System.out.println(xiaoming.age);
 *
 *         //这里我又创建了一个xiaohong 但没给值一个输出null,0
 *
 *
 *         System.out.println(xiaohong.name);//null
 *         System.out.println(xiaohong.age);//0
 *
 *
 *     }
 */

Person的类

package com.oop.dome02;

public class Person {

    //一个类即使什么都不写,它也会存在一个方法
    //显示的定义构造器
    //必须没有返回值也没有void

    String name;
    //实例化初始值
    //使用new关键字,本质是在调用构造器
//    public Person(){//构造器 无参
//        this.name = "好家伙";
//    }
    //有参构造:一旦定义了有参构造器,无参就必须显示定义
    //不然就会报错
    //有 有参,无参可以默认什么都不用写
    //用来初始化值的
    public Person(){//不用写
         }
    public Person(String name){//有参
        this.name = name;//

    }
    //生成构造器的快捷键alt+insert键
    //OK生成一个有参
    //select None(空出选项的意思)生成一个无参
}
/*
 public static void main(String[] args) {
        //new 实例化了一个对象

        Person person = new Person();//这一步会跳转到实例化输出里
        //如果调用的Person实例化没有值会输出null
        System.out.println(person.name);
    }


 */

创建对象分析

Application

package com.oop.dome03;
//创建对象分析
public class Application {
    public static void main(String[] args) {
        Pet dog = new Pet();

        dog.name = "小狗";
        dog.age = 5;
        dog.shout();

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

        Pet cat = new Pet();

        cat.name = "小猫";
        cat.age = 4;
        cat.shoutcat();

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

    }
}

pet类

package com.oop.dome03;

public class Pet {
    public String name;
    public int age;

    public void  shout(){
        System.out.println("叫了一声");




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

封装

Application

package com.oop.Dome04;
//封装
/**
 * 1.提高程序的安全性,保护数据
 * 2.隐藏代码的实现细节
 * 3.统一接口
 * 4.系统可维护增加l
 */
public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();
        //s1.name;//private 调用不了因为私有了
        //但换成 public就可以

        s1.setName("Hoge");

        System.out.println(s1.getName());

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

    }
}

Student

package com.oop.Dome04;

//类     private:私有
public class Student {
    //属性私有
    private String name;//名字
    private int id;//学号
    private char gender;//性别
    private int age;

    //提供一些可以操作这种的方法
    //提供一些public 的 get、set方法
    //不能编辑私有就用共有的public 给的方法去编辑private

    //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 getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if ( age<120 && age>0){
            this.age = age;
        }else {

            this.age = 0;

        }

    }
}

什么是继承、Supen详解、方法的重写

ApplicationAB

package com.oop.dome05;

public class ApplicationAB {
   //静态的方法和非静态的方法区别很大
    //静态方法: //等等的调用只和左边,定义的数
   public static void main(String[] args) {
       A a = new A();
       a.text();

       //父类的引用指向子类
       B b = new A();
       b.text();//B
   }

}

A

package com.oop.dome05;

public class A extends B {
    public  void text() {
        System.out.println("A=>text()");
    }
}


B

package com.oop.dome05;

public class B {
    //重写都是方法的重写,和属性无关

    public void text(){
        System.out.println("B=>text()");
    }
}

Application

package com.oop.dome05;

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.test("好家伙");
    }
}

People

package com.oop.dome05;
//父类
//在java中,所有的类,都默认直接或间接继承Object类

public class People {
    private int money = 10_0000_0000;
    public  void say(){
        System.out.println("说了一句");
    }
    public  int getMoney(){
        return money;
    }
    public void  setMoney(int money){
        this.money = money;
    }
protected String name ="Hoge";
}

Student

package com.oop.dome05;
//子类继承父类 学生类继承人类
//子类继承父类 就会继承父亲所有的,除了受保护的私有类等等
public class Student extends People{
    public Student(){
        //隐藏代码;调用父类的结构器
        super();//调用父类的结构器,必须在子类的第一行

        System.out.println("Student无参执行了");
    }
     private String name = "lizjian";
     public void test(String name ){
         System.out.println(name);//Application拿到的值
         System.out.println(this.name);//当前name的值
         System.out.println(super.name);//父类中拿到的值
     }
}

Teacher

package com.oop.dome05;

public class Teacher extends People{
}

总结

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

Vs this:
代表的对象不同:
this: 本身调用者这个对象
super: 代表父类对象的应用
前提:
this: 没有继承也可以使用
super:只有在继承条件下才可以使用
构造方法
this();本类的构造
super();父类的构造
重写:需要有继承关系,子类重写父类的方法!
1.方法名必须相同
2.参数列表必须相同
3.修饰符:范围可以扩大但不能缩小: public>protected>Deault>private
4. 抛出的异常:范围可以被缩小,但不能扩大;ClassNotFoundException -->Excption(大)
重写,子类的方法和父类必须一致;方法体不同!

为需要重写:
1.父类的功能,子类不一定需要,或者不一定满足!
Alt + insert ; override;

多态、instanceof和类型转换

Application

package com.oop.dome06;

public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定
        //new Student();
        //new Person();
        //可能指向的引用类型就不确定了:父类的引用指向子类

        //Student 等调用的方法都是自己的或者继承父类的!
        Student s1 = new Student();
        //Person 父类型,可以指向子类,但是不能调用子类独有的方法
        Teacher s2 = new Teacher();
        Object s3 = new Student();

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

}

Person

package com.oop.dome06;

public class Person {

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

    }
//    public void eat(){
//        System.out.println("eat");
//    }
}
/*
多态注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系  类型转换异常! ClassCastExcel
3.存在条件: 继承关系,方法需要重写,父类引用指向子类! Father f1 = new Son();
        //不能重写
        1.staic 方法,属于类,它不属于实例
        2.final 常量
        3.private方法;

 */

Student

package com.oop.dome06;

import com.oop.dome0601.Dog;

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

Teacher

package com.oop.dome06;

public class Teacher extends Person{
}

转换

Application
package com.oop.dome0601;

import javafx.animation.ScaleTransition;

public class Application {
   //类型之间的转化: 父  子
    //高            低
    Pets cat1 = new Cat();


   // ((Cat)cat).go();

}
/*
1.父类引用指向子类的对象
2.把子类转换为父类,向上转型;
3.把父类转换为子类,向下转型; 强制转换
4.方便方法的调用,减少重复的代码!简介

封装、继承、多态!  抽象类,接口
 */
Cat
package com.oop.dome0601;

public class Cat extends Pets {

}
/*
Applecation 运行
 public static void main(String[] args) {
        //Object > String
        //Object > Pets > Cat
        //Object > Pets > Dog
       // System.out.println(X instanceof Y);//要看xy有没有关系
        Object object = new Cat();
        System.out.println(object instanceof Cat);//true
        System.out.println(object instanceof Pets);//true
        System.out.println(object instanceof Object);//true
        System.out.println(object instanceof Dog);//false
        System.out.println(object instanceof String);//false
        System.out.println("========================================");
        Pets cat = new Cat();
        System.out.println(cat instanceof Cat);//true
        System.out.println(cat instanceof Pets);//true
        System.out.println(cat instanceof Object);//true
        System.out.println(cat instanceof Dog);//false
       // System.out.println(cat instanceof String);//false
        Dog dog = new Dog();

        System.out.println(dog instanceof Pets);//true
        System.out.println(dog instanceof Object);//true
        System.out.println(dog instanceof Dog);//false
        //System.out.println(dog instanceof Cat);//编译报错
        // System.out.println(dog instanceof String);//编译报错
    }
 */
Dog
package com.oop.dome0601;

public class Dog extends Pets{

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

    }
//    public void eat(){
//        System.out.println("eat");
//    }
}
/*
多态注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系  类型转换异常! ClassCastExcel
3.存在条件: 继承关系,方法需要重写,父类引用指向子类! Father f1 = new Son();
        //不能重写
        1.staic 方法,属于类,它不属于实例
        2.final 常量
        3.private方法;

 */
Pest
package com.oop.dome0601;

public class Pets {
}

static关键字详解

Application

package com.oop.dome07;

public class Application {

}

Person

package com.oop.dome07;

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

Student

package com.oop.dome07;
//static
public class Student {
    private static int age;//静态的变量 多线程
    private double score;//非静态变量
    public void run(){

    }
    private static  void go(){

    }
    public static void main(String[] args) {
        go();
//        Student s1 = new Student();
//
//        System.out.println(Student.age);
//        System.out.println(s1.age);
//        System.out.println(s1.score);

    }
}

Test

package com.oop.dome07;
//静态导入包
import static java.lang.Math.random;
public class Test {
    public static void main(String[] args) {
        System.out.println(random());//可以不用写Math.random()
    }
}

抽象类

A

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

    }
}

Action

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


}

接口

TimeService


public interface TimeService {
    void timer();
}

UesrService

package com.oop.dome09;
//interface 定义的关键字,接口都需要有实现类
public interface UserService {
    //常量 public static final
    int age = 99;
    //接口中所有定义的方法其实都是抽象的public adstract
    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);

}

UesrServicelmpl

package com.oop.dome09;
//类 可以实现接口 implement 接口
//实现了接口的类,就需要重写接口中的方法
//接口就可以实现多继承
public class UserServicelmpl 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() {

    }
}

总结

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

内部类

Application

package com.oop.dome10;

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

Outer

package com.oop.dome10;

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

Test.java

package com.oop.dome10;

import com.oop.dome09.UserService;

public class Test {
    public static void main(String[] args) {
        //没有名字初始化,不用讲实例保存到变量中
        new Apple().eat();

        User user =new User(){
            @Override
            public void hello() {

            }
        };

    }

    }
    class Apple{
    public void eat(){
        System.out.println("1");
    }
    }
interface User{
void hello();
}
/*

    public static void main(String[] args) {
        //没有名字初始化,不用讲实例保存到变量中
        new Apple().eat();

        UserService userService = new UserService(){

        }
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值