一.继承
Java语言的继承:单继承
1.类和类之间的关系
(1)组合关系
公司和员工,学校和学生
(2)继承关系
学生和人
二.Object类
public class Object {private static native void registerNatives();static {registerNatives();}
1.finalize()
对象被JVM回收的一定会自动调用。
~当对象已满
while (true) {A a = new A();
}
规则:当对象没有任何引用何其关联
A a = new A();
a = null;
package com.ffyca.entity;public class A {public void finalize() throws Throwable {System.out.println("我悄悄的走了,不带走一片树叶...");}
}
package com.ffyca.Test;import com.ffyca.entity.A;public class TestA {public static void main(String[] args) {A a = new A();a = null;System.gc();}
}
2.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());
3.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.)
4.==
判断两个引用是否指向同一个对象。
5.equals
public boolean equals(Object obj) {return (this == obj);}
判断内容相等
==比较的是字符串的地址,但因为有字符串常量池的存在,故str1,str2指向的地址是相同的,所以返回true
如果是以new String("java")的方式创建对象,那么无论内容是否相等,==判断返回的都是false
改写equals()
写一个汽车类:汽车牌号,品牌,价格,颜色
我们认为:如果两个汽车的牌号相同那么汽车相等
a.equals(b)
Tip:尽量不要手写equals和hashcode
package com.ffyca.entity;import java.util.Objects;public class Car {private String num;private String brand;private double price;private String color;public String getNum() {return num;}public void setNum(String num) {this.num = num;}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 String getColor() {return color;}public void setColor(String color) {this.color = color;}public Car(String num, String brand, double price, String color) {this.num = num;this.brand = brand;this.price = price;this.color = color;}@Overridepublic boolean equals(Object obj) {Car c2 = (Car)obj;String s1 = this.getNum();String s2 = c2.getNum();return s1.equals(s2);}@Overridepublic String toString() {return "Car{" +"num='" + num + '\'' +", brand='" + brand + '\'' +", price=" + price +", color='" + color + '\'' +'}';}
}
package com.ffyca.Test;import com.ffyca.entity.Car;public class TestCar {public static void main(String[] args) {Car car1 = new Car("陕F12345","BWM",2000000.0,"绿色");Car car2 = new Car("陕F12345","BWM",2000000.0,"绿色");System.out.println(car1.equals(car2));}
}
四.final关键词
1.修饰引用
c永远绑定此对象
final Car c = new Car();
2.变量
a永远是3
final int a = 3;
3.属性
只能通过构造器,或者属性直接给值
public class E{private final int a;
}
4.class
不能被继承
public final class E {
}
5.方法
子类,实现类不能重写的方法
public final void run(){}
五.接口企业中的应用
1.定规则
2.常量
public interface Constants {int SUNDAY = 7;int LAST_MONDAY = 1;
}
六.String
1.包
java.lang包下的类会自动引用
2.类
3.内部实现
4.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")
5.String常用的api
(1)将基本类型转换为字符串
Integer.parseInt(str)
Double.parseDouble(str)
char[] s = {'s','a','p','t'};String t = String.valueOf(s,0,3); System.out.println(t);
(2)字符大小转换,空格处理
(2.1)空格处理
输入时不小心多输入空格
username: admin
过滤掉前后空格 trim()
(2.2)大小转换
String str ="apple";str = str.toUpperCase();System.out.println("str:"+str);
3.字符串截取
substring(int,int);
包头不包尾,索引从0开始
substring(int);
substring(int,int);的重载方法,从当前索引位置一直截取到字符串的末尾
String str = "1000-赵云-蜀国-武将"; System.out.println(str.substring(8)); System.out.println(str.substring(8, 10));
4.查找子字符串
5.拆分字符串
把字符串按照某个指定内容分割成多个字符串,返回一个字符串数组
6.startwith
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("其它文件");}}
}
7.替换
8.正则表达式
(1)[a-z]
a-z的其中一个字符
(2)[0-9]
[0-9]的一个数字
[0-9]{17}[0-9Xx]
(3){n}
前面的数据出现的次数
[0-9Xx]:数字0-9或者X或者x
(4){m,n}
9.charAt()
获取某个索引位置的字符
String str = "apple";
System.out.println(str.charAt(3));//"p"
10.contains()
判断字符串中是否包含str
public class StringTest03 {public static void main(String[] args) {String str = "01234567";System.out.println(str.contains("2"));}
}
11.toCharArray()
将字符串转换为字符数组
public class StringTest03 {public static void main(String[] args) {String str = "01234567";char[] chars = str.toCharArray();for (int i = 0;i < chars.length;i++){System.out.println(chars[i]);}}
}