300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 漫漫Java学习路 第十一天

漫漫Java学习路 第十一天

时间:2021-11-13 04:03:21

相关推荐

漫漫Java学习路 第十一天

package cn.tedu.api;import java.util.Objects;//本类用于顶级父类Object的入门案例//1.查API手册//2.连点两下shift打开IDEA的搜索,注意勾选"include non-project items",再搜Object//3.在拓展库Externa librabies找到jdk1.8->rty.jar->public class TestObject {public static void main(String[] args) {Student s = new Student();Student s1 = new Student("海绵宝宝",3);Student s2 = new Student("海绵宝宝",3);//5.测试hashCode/*本方法的作用是返回对应对象的int类型的哈希码值* 本方法力求不同的对象返回的哈希码值不同* 这样我们就可以根据哈希码值区分不同的对象*/System.out.println(s.hashCode());System.out.println(s1.hashCode());System.out.println(s2.hashCode());//6.测试toString//打印s对象的是println(),这个方法会层层调用,一直到Object中的toString()/*Object中toString()的默认实现:对象的地址值[对象类型@十六进制的哈希码值]* 子类重写了toString()以后,打印的是:对象的类型+属性+属性值*/System.out.println(s);System.out.println(s1);//8.测试equals()/*Object中的equals()的默认实现使用的是==比较* ==比较的是左右两边的值,如果是基本类型,比较的就是字面值,比如1和1,3.4和3.4* 如果是引用类型,比较的是引用类型变量保存的地址值* 子类重写了equls()与hashCode()以后,比较的就是对象类型+属性+属性值*/System.out.println(s1.equals((s2)));//true}}class Student{String name;int age;public Student(){System.out.println("醒醒,你清醒一点");}public Student(String name, int age) {this.name = name;this.age = age;System.out.println("生前何必多睡,死后自会长眠");}//7.在Student类中添加重写的toString()方法@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}//9.添加重写的eqylas与hashcode/*equals()与hashCode()逻辑要保持一致,要重写都重写,要不重写,都不重写* Object默认实现:hashCode()的哈希码值根据地址值生成*equals()底层使用==比较两个对象的地址值* Student类重写后:hashCode()的哈希码值根据重写后传入的对象的属性生成*equals()比较两个对象的类型+所有属性与属性值*/@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age &&Objects.equals(name, student.name);}@Overridepublic int hashCode() {return Objects.hash(name, age);}}

package cn.tedu.api;import java.util.Objects;import java.util.concurrent.Callable;//本类用于回顾Object常用方法的使用public class TestObject2 {public static void main(String[] args) {Cat c1 = new Cat();Cat c2 = new Cat();Cat c3 = new Cat("机器猫",300,10000.99,"大雄",'男');Cat c4 = new Cat("机器猫",300,10000.99,"大雄",'男');Dog d1 = new Dog();Dog d2 = new Dog();Dog d3 = new Dog("旺财",5,500);Dog d4 = new Dog("旺财",5,500);System.out.println(c1);System.out.println(d2);System.out.println(c3.equals(c4));//重写后equals()不用==比较,不比较地址值,比较类型 属性 属性值System.out.println(d3 == d4);//false,==永远比较的是引用类型保存的地址值System.out.println(d3.equals(d4));//true}}class Cat{String name;int age;double price;String host;char gender;public Cat(){System.out.println("我们一起学猫叫");}public Cat(String name, int age, double price, String host, char gender) {this.name = name;this.age = age;this.price = price;this.host = host;this.gender = gender;System.out.println("一起喵喵喵喵喵");}}class Dog{String name;int age;double price;public Dog(String name, int age, double price) {this.name = name;this.age = age;this.price = price;System.out.println("我可能不是人,但你是真的狗~");}public Dog() {System.out.println("劳资是萨摩不是白狐!");}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Dog dog = (Dog) o;return age == dog.age &&pare(dog.price, price) == 0 &&Objects.equals(name, dog.name);}@Overridepublic int hashCode() {return Objects.hash(name, age, price);}@Overridepublic String toString() {return "Dog{" +"name='" + name + '\'' +", age=" + age +", price=" + price +'}';}}

package cn.tedu.api;//本类用于测试String类的使用public class TestString {public static void main(String[] args) {/*1.字符串类型底层维护的是char[]*/char[] value = {'a','b','c'};String s1 = new String(value);String s11 = new String(value);String s2 = "abc";String s22 = "abc";String s3 = "ccc";System.out.println(s1 == s2);System.out.println(s1 == s11);System.out.println(s2 == s22);System.out.println(s2 == s3);/*Object类中equals()的默认实现是通过==来比较的* 但是,String类已经重写过了继承字Object中的equals()* 重写后,不在按照==比较,而是比较两个字符串的具体内容* 也就是说,不论创建方式,只要串的内容一致,equals()就返回true*/System.out.println(s1.equals(s2));System.out.println(s1.equals(s11));System.out.println(s2.equals(s3));}}

package cn.tedu.api;import .apache.xalan.internal.xsltc.trax.XSLTCSource;import java.util.Arrays;//本类用来测试String类的常用方法public class TestString2 {public static void main(String[] args) {char[] values = {'a','b','c'};String s1 = new String(values);String s2 = "abc";/*String重写了hashCode(),是根据字符串的具体内容生成哈希码值,而不是根据地址值生成* 所以虽然s1与s2的地址值不同,但是他们的哈希码值一样*/System.out.println(s1.equals(s2));System.out.println(s1.hashCode());System.out.println(s2.hashCode());/*String也重写了Object中的toString(),打印的是字符串的具体内容*/System.out.println(s1);System.out.println(s1.length());System.out.println(s1.toUpperCase());//ABC,将本字符串转为全大写System.out.println(s1.toLowerCase());//abc,将本字符串转为全小写System.out.println(s1.startsWith("a"));//true,判断本字符串是否以指定元素a开头System.out.println(s1.endsWith("a"));//false,判断本字符串是否以指定元素a结尾System.out.println(s1.charAt(0));//a,根据下标获取本字符串中对应位置上的元素String s3 = "abcbdbba";System.out.println(s3.indexOf("b"));//1,返回指定字符第一次出现的下标System.out.println(s3.lastIndexOf("b"));//6,返回指定元素最后一次出现的下标System.out.println(s2.concat("cxy"));//abccxy,将指定字符串拼接到本字符串的结尾System.out.println(s2);//abc,说明上面的拼接是临时的,不会改变原串s2的内容String s4 = s2.concat("aaa");//如果想要多次使用拼接后的结果,需要定义一个字符串保存结果System.out.println(s4);//abcaaa//返回值类型是String[],所以需要Arrays.toString()打印//以指定字符作为分割符,分割当前的字符串String s5 = "afbfcfdfe";String[] a = s5.split("f");System.out.println(Arrays.toString(a));//[a, b, c, d, e]//我们也可以遍历数组,拿到数组中每个元素for (int i = 0; i < a.length; i++) {System.out.println(a[i]);}String s6 = "hh hhh ";System.out.println(s6.trim());//hh hhh,去除字符串首尾两端的空格String s7 = "abcdefgh";System.out.println(s7.substring(3));//defgh,从指定下标处开始截取子串[3,结束]System.out.println(s7.substring(3,6));//def,从指定下标处开始截取子串[3,6)含头不含尾System.out.println(20+10);//30,int+int-->计算效果System.out.println("20"+10);//,String+int-->拼接效果System.out.println(String.valueOf(10));//10System.out.println(String.valueOf(80)+10);//8010,将int类型的80转为String类型byte[] bs = s7.getBytes();//将指定字符串转为byte[]System.out.println(Arrays.toString(bs));}}

package cn.tedu.api;//本类用于测试字符串的拼接public class TestString3 {public static void main(String[] args) {String s = "abcdefghijklmnopqrstuvwxyz";//method1(s);method2(s);}private static void method2(String s) {//StringBuffer sb = new StringBuffer();StringBuilder sb2 = new StringBuilder();long t1 = System.currentTimeMillis();for (int i = 0; i < 100000 ; i++) {//sb.append(s);sb2.append(s);}long t2 = System.currentTimeMillis();//System.out.println(sb);System.out.println(sb2);System.out.println(t2 - t1);}private static void method1(String s) {String result = "";long t1 = System.currentTimeMillis();for (int i = 0; i < 10000 ; i++) {result = result+s;}long t2 = System.currentTimeMillis();System.out.println(result);System.out.println(t2 - t1);}}

package cn.tedu.api;import java.util.Arrays;//本类用于完成String类常用方法的复写public class ZuoYe {public static void main(String[] args) {String s1 = "abcd";String s2 = "abcde";String s3 = " abasujdc dsafgds ";System.out.println(s1.hashCode());//返回字符串的哈希码值System.out.println(s1.equals(s2));//将此字符串与指定对象比较,比较的是重写后的串的具体内容System.out.println(s1.toString());//返回此对象本身System.out.println(s2.length());//返回此字符串的长度System.out.println(s2.toUpperCase());//所有字符转化为全大写System.out.println(s2.toLowerCase());//所有字符转为全小写System.out.println(s1.startsWith("a"));//测试此数组是否以指定元素开头System.out.println(s1.endsWith("e"));//测试此数组是否以指定的字符结束System.out.println(s3.charAt(5));//返回指定索引/下标处的char值/字符System.out.println(s3.indexOf("d"));//返回指定字符在此字符串中第一次出现处的索引System.out.println(s3.lastIndexOf("d"));//返回指定字符在此字符串中最后一次出现处的索引System.out.println(s1.concat("dsawq"));//将指定字符串连接/拼接到此字符串的结尾,注意:不会改变原串System.out.println(Arrays.toString(s3.split("d")));//根据给定元素来分割此字符串System.out.println(s3.trim());//返回去除首尾空格的字符串byte[] b = s2.getBytes();//把字符串存储到一个新的byte数组中System.out.println(s3.substring(5));//返回一个新子串,从指定下标处开始,包括指定下标System.out.println(s3.substring(5,12));//返回一个新子串,从指定下标开始,到结束下标为止,但不包括结束下标[5,12)System.out.println(String.valueOf(5)+15);//把int转成String}}

package cn.tedu.api;import java.util.Arrays;//本类用于单层for循环的冒泡排序public class ZuoYe1 {public static void main(String[] args) {int[] a = {25,15,564,19,87,49,1564,12,748};int j = a.length-1;for (int i = 0; i < j ; i++) {int k = a[i];if(a[i]>a[i+1]){a[i] = a[i+1];a[i+1] = k;}if(i == j-1){i = -1;j--;System.out.println(Arrays.toString(a));}}System.out.println(Arrays.toString(a));}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。