Java继承

目标

继承复习

万类只祖(Object)

final

String :第九大类

python: 字符串工程师

常用的api

继承

Java语言是继承:单继承

类和类之间的关系

组合关系

公司和员工, 学校和学生

继承关系

学生和人,

Object类

public class Object {

​ private static native void registerNatives();
​ static {
​ registerNatives();
​ }

finalize()

对象被JVM回收的一定会自动调用。

  • 当对象已满
  while (true) {
            A a = new A();
        }

规则:当对象没有任何引用何其关联

A a = new A();

a= null;

toString()

Returns a string representation of the object. In general, the toString method returns a string that “textually represents” this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

System.out.println(c1)
System.out.println(c1.toString())

com.ffyc.entity.A@hashcode

开发过程中重写类的toString()方法

hashcode()

对象的一个唯一值,用来快速定位内存地址

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.
The general contract of hashCode is:
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)

image-20240519130743138

==

判断两个引用是否指向同一个对象。

equals()

public boolean equals(Object obj) {
    return (this == obj);
}

判断内容相等

改写equals()

写一个汽车类: 汽车牌号,品牌,价格,颜色
我们认为: 如果两个汽车的牌号相同那么汽车相等

a.equals(b)

Tips: 尽量不要手写equals和hashcode

image-20240519134253002

final关键字

修饰引用

c永远绑定此对象

final Car c = new Car();

c= null;

变量

a永远是3

final int a = 3;

属性

只能通过构造器,或者属性直接给值

public class E{
   private final int a;

}

class

不能被继承

public final class E {
}

方法

子类、实现类不能重写的方法

  public  final  void run(){

    }

this, super, static, final

接口企业中的应用

定规则

常量

/**
 * 定义常量
 */
public interface Constants {
    int SUNDAY=7;
    int LAST_MONDAY=1;
}

String

java.lang 包下的类会自动引入

image-20240519141136131

内部实现

image-20240519141318852

StringBuilder

提高字符串的拼接能力

StringBuilder sb = new StringBuilder();
sb.append("hello");

long s1 = System.currentTimeMillis();
for (int i=0;i<1000000;i++){
    sb.append(" world");
}
long s2 = System.currentTimeMillis();
System.out.println("时间:"+(s2-s1)+"ms")

image-20240519150343934

sb.toString()---->String

String常用的api

将基本类型转换为字符串

Integer.parseInt(str)

Double.parseDouble(str)

image-20240519150935753

        char[] s = {'s','a','p','t'};

        String t = String.valueOf(s,0,3);

        System.out.println(t);

字符大小转换,空格处理

image-20240519152326597

空格处理

输入时不小心多输入空格

username: admin

过滤掉前后空格 trim()

大小转换
        String str ="apple";
        str = str.toUpperCase();
        System.out.println("str:"+str);

字符串截取

image-20240519153433239

        String str = "1000-赵云-蜀国-武将";

        System.out.println(str.substring(8));
        System.out.println(str.substring(8, 10));

查找子字符串

image-20240519154618534

image-20240519154708613

拆分字符串

image-20240519161811571

startwith

image-20240519161851665

image-20240519162255035

public class StringTest06 {
    public static void main(String[] args) {
        String str = "hello,this is beautiful HanZhong";
        String str2 = "xuexiao.jpgx";

        System.out.println(str.startsWith("this"));


        if(str2.endsWith(".jpg")||str.endsWith(".png")||str.endsWith(".bmp")){
            System.out.println("是图片");
        }else {
            System.out.println("其它文件");
        }

    }
}

替换

image-20240519162401875

正则表达式

[a-z]

a-z的其中一个字符

[0-9]

[0-9]的一个数字

[0-9]{17}[0-9Xx]
{n}

前面的数据出现的次数

[0-9Xx]: 数字0-9 或则X或者x

{m,n}

chatAt()

contains()

toCharArray()

em.out.println(“其它文件”);
}

}

}


### 替换

[外链图片转存中...(img-6MnUJbCh-1718514659653)]

### 正则表达式

#### [a-z]

> a-z的其中一个字符

#### [0-9]

> [0-9]的一个数字

[0-9]{17}[0-9Xx]


#### {n}

前面的数据出现的次数

[0-9Xx]: 数字0-9 或则X或者x

#### {m,n}

### chatAt()

### contains()

### toCharArray()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值