1. 封装

什么是面向对象?面向对象与面向过程的区别?面向对象的好处?自查资料学习

面向对象:纪传体 《史记》 以人物为核心 讲述人物的实际 并进行评论

面向过程:编年史 以历史事件发生的时间顺序 讲述历史

封装:

把过程和数据包围起来,对于数据的访问只能通过已经定义好的接口进行访问。封装把对象所有组成部分组合在一起,使用方法将类的数据隐藏起来,控制用户对于类的修改以及数据访问程度。

private修饰符

public class Girl {

    //age属性,加了private修饰符,外界对age属性的访问以及设置就收到了限制
private int age; public int getAge() {
return age;
} public void setAge(int age) { if (age >= 30) {
this.age = 18;
} else {
this.age = age;
}
}
}

练习:

public class Student {
private String name;
private int age;
private String sex; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getSex() {
return sex;
} public void setSex(String sex) {
if ("男".equals(sex) || "女".equals(sex)) {
this.sex = sex;
} else {
this.sex = "错误";
}
} public Student() {
} public Student(String name, int age, String sex) {
this.name = name;
this.age = age;
/*this.sex = sex;*/
this.setSex(sex);
}
}

package com.msb3;

/**

  • @Auther: jack.chen

  • @Date: 2023/9/10 - 09 - 10 - 11:25

  • @Description: com.msb3

  • @version: 1.0

    */

    public class Test {

    public static void main(String[] args) {

    Student s = new Student();

     System.out.println(s.getAge());
    System.out.println(s.getName());
    System.out.println(s.getSex()); Student s1 = new Student("lili", 18, "女22"); System.out.println(s1.getAge());
    System.out.println(s1.getName());
    System.out.println(s1.getSex());

    }

    }

2. 继承

学生 继承 Person

package com.msb4;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 11:33
* @Description: com.msb4
* @version: 1.0
*/
public class Person {
private int age;
private String name;
private double height; public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public double getHeight() {
return height;
} public void setHeight(double height) {
this.height = height;
} public void eat(){
System.out.println("eat");
}
public void sleep(){
System.out.println("sleep");
}
}
package com.msb4;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 11:35
* @Description: com.msb4
* @version: 1.0
*/
public class Student extends Person{ //学生独有的私有属性 学号sno
private int sno; public int getSno() {
return sno;
} public void setSno(int sno) {
this.sno = sno;
} public void study(){
System.out.println("study");
}
}
package com.msb4;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 11:37
* @Description: com.msb4
* @version: 1.0
*/
public class Test { public static void main(String[] args) {
Student s = new Student();
s.setSno(1010);
s.setAge(18);
s.setName("feifei");
s.setHeight(188.9); s.study();
s.sleep();
s.eat();
} }
  1. 一个父类可以有多个子类
  2. 一个子类只能有一个直接父类,但是可以继承自其他的父类
  3. 继承具有传递性

    Student-->Person --> Object

    Object是所有类的祖宗,也就是所有的类都直接或者间接继承自object

权限修饰符

private 只能是同一个类才能访问

default int age;默认不写的权限 只能同一个包才能访问

protected 子类才能访问

public 都可以

写代码的一般规则:

一般属性用 private修饰

方法都用public 修饰

2.1 什么是方法的重写

对父类提供的方法不满足的时候 对父类的方法进行重写

重写父类的方法有严格限制与要求:

方法名必须一直,参数列表(个数 类型 顺序)也要一致

package com.msb5;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 11:51
* @Description: com.msb5
* @version: 1.0
*/
public class Person { public void eat(){
System.out.println("eat");
} public void sleep(){
System.out.println("sleep");
} }
package com.msb5;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 11:56
* @Description: com.msb5
* @version: 1.0
*/
public class Student extends Person{
public void study(){
System.out.println("study");
} @Override
public void eat(){
System.out.println("eat 小龙虾");
}
}
package com.msb5;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 11:59
* @Description: com.msb5
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
Student s = new Student(); s.eat();
}
}

2.2 super

  1. super访问父类的属性 访问父类的方法
package com.msb5;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 11:51
* @Description: com.msb5
* @version: 1.0
*/
public class Person {
int age; public void eat(){
System.out.println("eat");
} public void sleep(){
System.out.println("sleep");
} }
package com.msb5;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 11:56
* @Description: com.msb5
* @version: 1.0
*/
public class Student extends Person{
double score; public void study(){
System.out.println("study");
} @Override
public void eat(){
System.out.println("eat 小龙虾");
} public void a(){
System.out.println(super.age);
super.eat();//调用父类的eat方法 } public static void main(String[] args) {
Student s = new Student();
s.a(); }
}

  1. 构造器中使用super()
package com.msb5;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 11:51
* @Description: com.msb5
* @version: 1.0
*/
public class Person {
int age;
String name; public Person(){ }
public Person(int age, String name){
this.name = name;
this.age = age;
} public void eat(){
System.out.println("eat");
} public void sleep(){
System.out.println("sleep");
} }
package com.msb5;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 11:56
* @Description: com.msb5
* @version: 1.0
*/
public class Student extends Person{
double score; public Student(){ } public Student(int age, String name, double score){
super(age, name);//调用父类的构造器
/*this(score);*/ //构造器只能调用一个 因为实例化对象 只能实例化一个对象
this.score = score;
} public void study(){
System.out.println("study");
} @Override
public void eat(){
System.out.println("eat 小龙虾");
} public void a(){
System.out.println(super.age);
super.eat(); } public static void main(String[] args) {
Student s = new Student(18, "lili", 120.1);
s.a();
System.out.println(s.age);
System.out.println(s.name);
System.out.println(s.score); }
}

  1. 使用idea的快捷键alt+insert快速生成构造

    快捷键生成构造器 alt+insert
package com.msb6;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 21:34
* @Description: com.msb6
* @version: 1.0
*/
public class Student extends Person {
double height;
double weight; public Student(){ } public Student(int age, String name) {
super(age, name);
} public Student(int age, String name, double height, double weight) {
super(age, name);
this.height = height;
this.weight = weight;
}
}

2.3 object详解

object是java中一切类的根基

所有未声明其父类的class默认 extended object

toString方法

package com.msb6;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 21:34
* @Description: com.msb6
* @version: 1.0
*/
public class Student extends Person {
double height;
double weight; public Student(){ } public Student(int age, String name) {
super(age, name);
} public Student(int age, String name, double height, double weight) {
super(age, name);
this.height = height;
this.weight = weight;
} public String toString(){//重写toString方法
return getClass().getName() + "@" + Integer.toHexString(hashCode()); }
}
package com.msb6;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 21:34
* @Description: com.msb6
* @version: 1.0
*/
public class Student extends Person {
double height;
double weight; public Student(){ } public Student(int age, String name) {
super(age, name);
} public Student(int age, String name, double height, double weight) {
super(age, name);
this.height = height;
this.weight = weight;
} public String toString(){
// return getClass().getName() + "@" + Integer.toHexString(hashCode());
return "这个是一个Student对象, 名字是"+name+"身高:"+height+"体重"+weight;
}
}
package com.msb6;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 21:41
* @Description: com.msb6
* @version: 1.0
*/
public class Test { public static void main(String[] args) {
Student s = new Student(18, "lili", 188.8, 68.9); System.out.println(s.toString());
} }

toString方法就是对象的自我介绍,

可以用快捷键生成toString方法

public class Student extends Person {
double height;
double weight; public Student(){ } public Student(int age, String name) {
super(age, name);
} public Student(int age, String name, double height, double weight) {
super(age, name);
this.height = height;
this.weight = weight;
} /* public String toString(){
// return getClass().getName() + "@" + Integer.toHexString(hashCode());
return "这个是一个Student对象, 名字是"+name+"身高:"+height+"体重"+weight;
}*/ @Override
public String toString() {
return "Student{" +
"height=" + height +
", weight=" + weight +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}

alt+insert

2.4 equals方法

package com.msb;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 21:51
* @Description: com.msb
* @version: 1.0
*/
public class Phone {
private String brand;
private double price;
private int year; public Phone() {
} public Phone(String brand, double price, int year) {
this.brand = brand;
this.price = price;
this.year = year;
} public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} public int getYear() {
return year;
} public void setYear(int year) {
this.year = year;
} @Override
public String toString() {
return "Phone{" +
"brand='" + brand + '\'' +
", price=" + price +
", year=" + year +
'}';
} public boolean equals(Object obj){//重写equals方法
Phone other = (Phone) obj;//向下转型
if(this.getBrand().equals(other.getBrand())&&this.getYear()==other.getYear()){
return true;
}
return false; }
}
package com.msb;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 21:53
* @Description: com.msb
* @version: 1.0
*/
public class Test {
public static void main(String[] args) { Phone p1 = new Phone("小米", 199.9, 2019);
Phone p2 = new Phone("小米", 199.9, 2019); System.out.println(p1==p2); //false对于引用数据类型存储的是地址 两个不同的对象肯定不一样
System.out.println(p1.equals(p2)); }
}

使用快捷键alt+insert生成equals方法

package com.msb;

import java.util.Objects;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 21:51
* @Description: com.msb
* @version: 1.0
*/
public class Phone {
private String brand;
private double price;
private int year; public Phone() {
} public Phone(String brand, double price, int year) {
this.brand = brand;
this.price = price;
this.year = year;
} public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} public int getYear() {
return year;
} public void setYear(int year) {
this.year = year;
} @Override
public String toString() {
return "Phone{" +
"brand='" + brand + '\'' +
", price=" + price +
", year=" + year +
'}';
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Phone)) return false;
Phone phone = (Phone) o;
return Double.compare(phone.getPrice(), getPrice()) == 0 &&
getYear() == phone.getYear() &&
Objects.equals(getBrand(), phone.getBrand());
} @Override
public int hashCode() {
return Objects.hash(getBrand(), getPrice(), getYear());
}
}
    @Override
public boolean equals(Object o) {
if (this == o) return true;//如果是自己 返回true
if (!(o instanceof Phone)) return false; //如果不是同一个类实例化出来的 返回false
Phone phone = (Phone) o;//使同一个类实例化的 将o这个对象 向下转型成Phone对象
return Double.compare(phone.getPrice(), getPrice()) == 0 &&
getYear() == phone.getYear() &&
Objects.equals(getBrand(), phone.getBrand());
//浮点数的比较用Double.compare
//int类型 直接 ==比较 这里都省略了this.
//字符串的比较用Objects.equals()
}

类之间的关系

package com.msb2;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 22:23
* @Description: com.msb2
* @version: 1.0
*/
public class Boy {
String name;
int age; public Boy(String name, int age) {
this.name = name;
this.age = age;
} public void buy(){
System.out.println("买买买!!");
}
}
 package com.msb2;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 22:22
* @Description: com.msb2
* @version: 1.0
*/
public class Girl {
String name;
double weight;
Mom m;//女生的妈妈是谁 了解下丈母娘的背景!!!
public Girl(String name, double weight) {
this.name = name;
this.weight = weight;
} public void love(Boy b){
System.out.println("我的男朋友是+"+b.name);
b.buy(); }
}
package com.msb2;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 22:27
* @Description: com.msb2
* @version: 1.0
*/
public class Mom {
public void say(){
System.out.println("妈妈总是唠唠叨叨");
}
}
package com.msb2;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 22:25
* @Description: com.msb2
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
Boy b = new Boy("明星A", 18);
Girl g = new Girl("xiaotong", 65);
g.love(b); //girl的妈妈 属性为赋值 报错
g.m.say(); }
}

package com.msb2;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 22:25
* @Description: com.msb2
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
Boy b = new Boy("明星A", 18);
Girl g = new Girl("xiaotong", 65);
g.love(b); g.m = new Mom();//给girl对象赋值妈妈属性 g.m.say(); }
}

类与类之间的关系:

常见的:继承 实现

组合>聚合>关联>依赖 可baidu了解

3. 多态

多态指的是 不同的子类 重写父类的方法 从而有了不同的实现方式也就是 方法的多态

package com.msb3;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 22:50
* @Description: com.msb3
* @version: 1.0
*/
public class Dog extends Animal {
public void shout(){
System.out.println("旺旺汪"); } public void guard(){
System.out.println("看门"); }
}
package com.msb3;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 22:48
* @Description: com.msb3
* @version: 1.0
*/
public class Cat extends Animal { public void shout(){
System.out.println("喵喵喵");
} public void scratch(){
System.out.println("我是小猫 我可以 抓 "); }
}
package com.msb3;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 22:50
* @Description: com.msb3
* @version: 1.0
*/
public class Dog extends Animal {
public void shout(){
System.out.println("旺旺汪"); } public void guard(){
System.out.println("看门"); }
}
package com.msb3;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 22:52
* @Description: com.msb3
* @version: 1.0
*/
public class Girl { public void play(Animal an){
an.shout();
}
}
package com.msb3;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 22:51
* @Description: com.msb3
* @version: 1.0
*/
public class Pig extends Animal {
public void shout(){
System.out.println("我是小猪 enenen 是我的叫声");
}
public void eat(){
System.out.println("吃吃吃"); }
}
package com.msb3;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 22:53
* @Description: com.msb3
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
Girl g = new Girl(); Pig p = new Pig();
Animal an = p; // 父类的应用指向子类的对象
g.play(an); Animal an1 = new Cat(); // 与上面是相同的
g.play(an1); Dog d = new Dog();
g.play(d); // 父类的应用指向子类的对象 传参的时候有转换 }
}

什么是向下转型 与 向上转型

package com.msb3;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 22:53
* @Description: com.msb3
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
Girl g = new Girl(); Pig p = new Pig();
Animal an = p; // 父类的应用指向子类的对象 向上转型
g.play(an); Animal an1 = new Cat(); // 与上面是相同的
g.play(an1); Dog d = new Dog();
g.play(d); // 父类的应用指向子类的对象 传参的时候有转换 //向下转型
Pig p1 = (Pig) an; //转成pig之后才有weight age属性
p1.eat();
p1.weight = 10;
p1.age = 20; }
}

简单的工厂模式

static 提供getAnimal方法

package com.msb3;

/**
* @Auther: jack.chen
* @Date: 2023/9/10 - 09 - 10 - 23:09
* @Description: com.msb3
* @version: 1.0
*/
public class PetStore { public static Animal getAnimal(String petName){
Animal an = null;
if ("猫".equals(petName)){
an = new Cat();
}
if ("猪".equals(petName)){
an = new Pig();
}
if ("狗".equals(petName)){
an = new Dog();
}
return an;
}
}

调用

        Animal an2 = PetStore.getAnimal("狗");
g.play(an2);

4. final修饰符

final的意思就是没有下文了到我这里截止

变量不能再赋值

类的method不能被子类重写

类被final修饰 则代表没有子类 (理解 断子绝孙 )

  1. 修饰变量
package com.msb4;

/**
* @Auther: jack.chen
* @Date: 2023/9/13 - 09 - 13 - 20:14
* @Description: com.msb4
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
// 1. 修饰变量
final int A = 10;
//A = 20; 不能在赋值 final Dog d = new Dog();//final修饰引用数据类型 地址就不可以在改变
// d = new Dog(); 不能再赋值修改 a(d);//可以 final Dog d2 = new Dog();
b(d2);
} public static void a(Dog d){
d = new Dog();
} public static void b(final Dog d){
//d = new Dog(); //提示final变量不能再赋值
}
}
  1. 修饰方法则该方法不能被该类的子类 重写

  1. 修饰类



  2. 出现的案例

    jdk源码里面

    package java.lang;

    public final class MAth{}

相当于这里面的东西不允许你修改 这是java语言提供的公共实现方法

例如pai=3.14

外面是不能

Math m = new Math();

被final修饰的类的方法全都被static修饰不能让外面修改

5.抽象类

作用是给子类提供一个通用的模板,子列的父类的基础上进行开发,重写父类的方法,扩展自己的内容,避免了子类的随意性

只要有一个抽象方法 这个类就必须变成抽象类

子类继承了抽象类就必须重写抽象类里面的方法





子类如果没有实现抽象类的方法 自己可以变成一个抽象类

抽象类是不可以创建对象的

6. 接口

接口是接口 类是类 他们是同层次的概念

接口没有构造器

格式:

[修饰符] interface 接口名 [extends 父接口1, 父接口2] {

常量;

方法;

}

注:

方法前面默认都是 public abstract 可以不写

常量前面默认public static final 可以不写

package com.msb6;

/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 9:25
* @Description: com.msb6
* @version: 1.0
*/
public interface TestInterface01 {
int Num = 1;
public abstract void a();
void b();
int c(String name);
}

类是接口的实现 -- 接口与类的关系

实现类要重写接口中的全部方法

如果没有全部重写 这个类可以变成抽象类

java只有单继承 但是有多实现

例如:

public class Person {
int age;
String name; public void sleep(){
System.out.println("sleeping"); }
}
package com.msb6;

/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 9:32
* @Description: com.msb6
* @version: 1.0
*/
//一个类只能直接继承一个类
//实现类实现接口的话 可以实现多个接口
public class Student extends Person implements TestInterface01,TestInterface02{
@Override
public void a() {
System.out.println("----1");
} @Override
public void b() {
System.out.println("----2"); } @Override
public int c(String name) {
return 0;
} @Override
public void e() {
System.out.println("----3"); } @Override
public void f() {
System.out.println("----4"); }
}

package com.msb6; /**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 9:25
* @Description: com.msb6
* @version: 1.0
*/
public interface TestInterface01 {
public static final int Num = 1;
int Num2 = 2; public abstract void a();
void b();
int c(String name);
}
package com.msb6;

/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 9:29
* @Description: com.msb6
* @version: 1.0
*/
public interface TestInterface02 {
void e();
void f();
}

package com.msb6; /**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 9:39
* @Description: com.msb6
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
// TestInterface01 t1 = new TestInterface01(); // 接口类不能创建对象 System.out.println(TestInterface01.Num);
System.out.println(Student.Num);
Student s = new Student();
System.out.println(s.name);
System.out.println(s.Num); TestInterface01 t2 = new Student(); //多态父类对象指向子类的实现
System.out.println(t2.Num2); }
}

接口的作用:

定义规则 接口定义好规则 实现类负责实现

与抽象类的区别:

接口是接口 类是类

继承:

子类对父类的继承 extends

实现:

实现类对于接口的实现

举例:

手机不是照相机

如果用继承来写

手机 extends 照相机

前面讲过:继承 可以理解为 is a的关系 手机 is a 照相机 逻辑上不通

所以这个写法不好

实现:

手机 implements 照相机 has a的关系 手机 has a照相机的功能

其他案例:

飞机 小鸟 风筝

飞机是小鸟 小鸟 是风筝 都不通

只能是提取出公共的功能 具体的实现类区实现

定义一个接口 Flyable

多态:

  1. 父类当做方法的形参 传入的是具体的子类对象
  2. 父类当做方法的返回值 参会具体的子类对象
  3. 接口仿作方法的形参 传入具体的实现类的对象
  4. 接口当做方法的返回值 返回的具体的实现类的对象

7. 内部类

类的组成:

属性 方法 构造器 代码块 + 内部类

非静态的成员内部类

package com.msb7;

/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 10:07
* @Description: com.msb7
* @version: 1.0
*/
public class TestOuter {
//非静态 成员内部类
public class D{ int age = 20;
String name;
public void method(){
//内部类可以访外部类的内容(方法 属性)
System.out.println(age);
a();
int age = 30;
//内部 和外部类 属性重名是 如何调用
System.out.println(age);
System.out.println(this.age);
System.out.println(TestOuter.this.age);
}
} //静态 成员内部类
static public class E{
public void method(){
//静态 内部内 只能访问 外部类中 被static修饰的内容
// System.out.println(age);
// a(); System.out.println(height);
b(); }
} int age = 10;//属性
static int height = 188; static public void b(){
System.out.println("static b method");
} //方法 a
public void a(){
System.out.println("a method"); } static {
System.out.println("静态块");
} //构造器
public TestOuter() {
} public TestOuter(int age) {
this.age = age;
}
}
package com.msb7;

/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 10:12
* @Description: com.msb7
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
TestOuter testOuter = new TestOuter(); testOuter.a(); //静态内部类 创建对象
TestOuter.E e = new TestOuter.E(); // 非静态成员内部类创建对象
TestOuter t = new TestOuter();
TestOuter.D d = t.new D();
d.method();
}
}

静态 成员 内部类

非静态成员内部类

实例化对象的区别:

运行结果:

局部内部类

package com.msb08;

/**
* @Auther: jack.chen
* @Date: 2023/9/17 - 09 - 17 - 10:41
* @Description: com.msb08
* @version: 1.0
*/
public class TestOuter { public void method(){
final int num = 1; class A{
public void a(){
System.out.println(num);
}
} } //类B在真个项目中只使用一次 没必要单独创建一个类
public Comparable method2(){
class B implements Comparable{
@Override
public int compareTo(Object o) {
return 100;
}
}
return new B();
} // B的名字都不要了 匿名内部类
public Comparable method3(){
return new Comparable() {
@Override
public int compareTo(Object o) {
return 0;
}
};
} public void test(){
Comparable c = new Comparable() {
@Override
public int compareTo(Object o) {
return 0;
}
};
System.out.println(c.compareTo("aaa"));
} }

8. 面向对象 项目作业 饮料售货机--待续

java基础-java面向对象-02-day09的更多相关文章

  1. Java基础-初识面向对象编程(Object-Oriented-Programming)

    Java基础-初识面向对象编程(Object-Oriented-Programming) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Java是一门面向对象的程序设计语言.那么什 ...

  2. Java基础-Java中23种设计模式之常用的设计模式

    Java基础-Java中23种设计模式之常用的设计模式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.设计模式分类 设计模式是针对特定场景给出的专家级的解决方案.总的来说设 ...

  3. Java基础-JAVA中常见的数据结构介绍

    Java基础-JAVA中常见的数据结构介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是数据结构 答:数据结构是指数据存储的组织方式.大致上分为线性表.栈(Stack) ...

  4. java基础---->java中正则表达式二

    跟正则表达式相关的类有:Pattern.Matcher和String.今天我们就开始Java中正则表达式的学习. Pattern和Matcher的理解 一.正则表达式的使用方法 一般推荐使用的方式如下 ...

  5. Java基础-Java中的堆内存和离堆内存机制

    Java基础-Java中的堆内存和离堆内存机制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  6. Java基础-Java中的内存分配与回收机制

    Java基础-Java中的内存分配与回收机制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一. 二.

  7. Java基础-Java中的并法库之重入读写锁(ReentrantReadWriteLock)

    Java基础-Java中的并法库之重入读写锁(ReentrantReadWriteLock) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在学习Java的之前,你可能已经听说过读 ...

  8. Java基础-Java中的并法库之线程池技术

    Java基础-Java中的并法库之线程池技术 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是线程池技术 二.

  9. Java基础-Java数据类型

    Java基础-Java数据类型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据类型的作用 数据类型就是一组值,以及这一组值上的操作,数据类型可以决定数据的存储方式,取值范围 ...

  10. 黑马程序员——【Java基础】——面向对象(二)异常机制、包(Package)

    ---------- android培训.java培训.期待与您交流! ---------- 一.异常机制 (一)异常概述 1.异常:就是程序在运行时出现不正常情况. 2.异常类:程序在运行时,出现的 ...

随机推荐

  1. Markdown References

    每次写文章,用到都要查一下的Markdown语法 字体颜色 [Markdown笔记]设置字体颜色 字体颜色 Font colors Decrsiption Demonstration Effect 选 ...

  2. VsCode运行与freopen与快读

    运行 g++ -o a a.cpp && ./a g++ b.cpp -o b && ./b g++ c.cpp -o c && ./c freopen ...

  3. 华企盾DSC导致导出文件报错常见处理方法

    1.导出文件的进程和打开该文件的进程启用OLE控制是否都是未勾选,以及启用虚拟重定向是否设置一致(要么都勾选要么都不勾) 2.用procmon监控个人模式下导出非加密的文件,搜索writefile的进 ...

  4. python tkinter 使用(二)

    python tkinter 使用(二) 本篇文章着重讲下tkinter中messagebox的使用. 1:提示框 def showinfo(event): messagebox.showinfo(& ...

  5. 【C#】【IO】【实例】接上一个统计的新功能

    先用Python来创建多层级文件夹: import os root_path = r"C:\Users\Desktop\文案整理\Practice" for item in ran ...

  6. 面试官喜欢问Nacos原理?直接把这篇文章甩给他!

    大家好,我是三友~~ 今天就应某位小伙伴的要求,来讲一讲Nacos作为服务注册中心底层的实现原理 不知你是否跟我一样,在使用Nacos时有以下几点疑问: 临时实例和永久实例是什么?有什么区别? 服务实 ...

  7. Linux:LVM磁盘扩容实战

    1.列出分区表 命令:fdisk -l 查看到目前挂载了 vda1.vda2.vda3,并且查看到还有vdb 100G没有挂载,以及现在的LV大小仅为29G root@JumpServer-DB-T0 ...

  8. 合宙esp32 c3 micro python 固件配置(基于thonny)

    首先,本文档是已经配置过其他esp32后发现合宙的配置需要修改一些地方. 为了让新手们减少掉坑成本,故做了一个图文指导. 准备工作: 1.thonny安装(不讲,自己去查教程) 2.esp32c3 m ...

  9. k8s卷管理-2

    目录 k8s卷管理-2 pv和pvc pv pv的定义 pvc pvc的定义 pv和pvc的绑定关系 手动指定pv与pvc绑定 pod使用pvc pod挂载pvc 访问模式的区别 k8s卷管理-2 之 ...

  10. LeetCode 数、二叉树、二叉搜索树篇(94、144、145)

    94. 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? / ...