标签:理解 数值 jdk和jre boolean ons sop 包含 一个 字符串
封装:
1 package com.spring.test.vo; 2 3 /** 4 * @Author: philosopherZB 5 * @Date: 2019/9/28 6 * 将现实中客观存在的人,抽象成一个封装类Person 7 */ 8 public class Person { 9 //使用private隐藏name属性变量,仅提供get,set方法给外部调用以达到修改的目的 10 private String name; 11 12 public String getName() { 13 return name; 14 } 15 16 public void setName(String name) { 17 this.name = name; 18 } 19 }
继承:
1 package com.spring.test.service; 2 3 /** 4 * @Author: philosopherZB 5 * @Date: 2019/9/29 6 */ 7 public class Test { 8 public static void main(String[] args){ 9 Teacher teacher = new Teacher("老师"); 10 Doctor doctor = new Doctor("医生"); 11 teacher.eat(); 12 teacher.sleep(); 13 doctor.eat(); 14 doctor.sleep(); 15 } 16 } 17 18 //将现实中客观存在的人,抽象成一个封装类Person 19 class Person { 20 private String name; 21 22 public Person(String name){ 23 this.name = name; 24 } 25 public void eat(){ 26 System.out.println(name+"吃饭"); 27 } 28 public void sleep(){ 29 System.out.println(name+"睡觉"); 30 } 31 } 32 33 //老师是一个人,继承父类Person 34 class Teacher extends Person{ 35 //显示调用父类带参构造器 36 public Teacher(String name) { 37 super(name); 38 } 39 } 40 41 //医生是一个人,继承父类Person 42 class Doctor extends Person{ 43 //显示调用父类带参构造器 44 public Doctor(String name) { 45 super(name); 46 } 47 }
多态:
1 package com.spring.test.service; 2 3 /** 4 * @Author: philosopherZB 5 * @Date: 2019/9/29 6 */ 7 public class Test { 8 public static void main(String[] args){ 9 Teacher teacher = new Teacher("老师"); 10 teacher.eat(); 11 teacher.eat("零食"); 12 } 13 } 14 15 //将现实中客观存在的人,抽象成一个封装类Person 16 class Person { 17 private String name; 18 19 public Person(String name){ 20 this.name = name; 21 } 22 public void eat(){ 23 System.out.println(name+"吃饭"); 24 } 25 //同一类中对方法进行重载 26 public void eat(String s){ 27 System.out.println("重载吃"+s); 28 } 29 } 30 31 //老师是一个人,继承父类Person 32 class Teacher extends Person{ 33 //显示调用父类带参构造器 34 public Teacher(String name) { 35 super(name); 36 } 37 //父子类之间对方法进行重写 38 @Override 39 public void eat(){ 40 System.out.println("重写吃饭"); 41 } 42 }
final:
基本数据类型(四个整数,两个浮点,一个字符,一个布尔):
JDK(Java Development Kit):
JRE(Java Runtime Environment):
Math三个常用方法:
1 public class Test { 2 public static void main(String[] args){ 3 System.out.println(Math.ceil(1.5));//向上取整,输出2.0 4 System.out.println(Math.floor(1.3));//向下取整,输出1.0 5 System.out.println(Math.round(-1.5));//加0.5之后向下取整,输出-1(为了加深理解可以试一下传入-1.6,看看输出什么) 6 } 7 }
String:
StringBuilder:
StringBuffer:
String s = "Test":
String s = new String("Test"):
总结:
1 public class Test { 2 public static void main(String[] args){ 3 String s1 = "Test"; 4 String s2 = "Test"; 5 String s3 = new String("Test"); 6 String s4 = new String("Test"); 7 System.out.println(s1==s2);//true 8 System.out.println(s3==s4);//false 9 } 10 }
值传递:
引用传递:
总结:
1 public class Test { 2 public static void main(String[] args){ 3 //值传递 4 String s1 = "Test--1"; 5 change(s1); 6 System.out.println(s1);//输出Test--1 7 //引用传递 8 StringBuilder sb = new StringBuilder("str--1"); 9 change2(sb); 10 System.out.println(sb);//输出str--1--2 11 } 12 private static void change(String str){ 13 str = "Test--2"; 14 } 15 private static void change2(StringBuilder str){ 16 str.append("--2"); 17 } 18 }
= =:
equels:
关于Integer t1 = 128,Integer t2 = 128,t1==t2,返回false原因:
1 public class Test { 2 public static void main(String[] args){ 3 Integer t1 = 127; 4 Integer t2 = 127; 5 Integer t3 = 128; 6 Integer t4 = 128; 7 System.out.println(t1==t2); //true 8 System.out.println(t3==t4); //false 9 } 10 } 11 //以下结果来自反编译,可以看到使用的是Integer.valueOf()方法进行自动装箱 12 public class Test 13 { 14 public static void main(String[] args) { 15 Integer t1 = Integer.valueOf(128); 16 Integer t2 = Integer.valueOf(128); 17 System.out.println((t1 == t2)); 18 } 19 } 20 21 //以下源码来自于jdk8中的Integer类 22 public static Integer valueOf(int i) { 23 //如果在-128到127之间,则直接返回缓存中的数值,否则创建一个新的对象保存 24 if (i >= IntegerCache.low && i <= IntegerCache.high) 25 return IntegerCache.cache[i + (-IntegerCache.low)]; 26 return new Integer(i); 27 } 28 29 private static class IntegerCache { 30 static final int low = -128; //最小值为-128 31 static final int high; //最大值 32 static final Integer cache[]; //缓存存储数组 33 34 static { 35 // high value may be configured by property--最大值可以通过属性配置 36 int h = 127; //最大值127 37 String integerCacheHighPropValue = 38 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); 39 if (integerCacheHighPropValue != null) { 40 try { 41 int i = parseInt(integerCacheHighPropValue); 42 i = Math.max(i, 127); 43 // Maximum array size is Integer.MAX_VALUE 44 h = Math.min(i, Integer.MAX_VALUE - (-low) -1); 45 } catch( NumberFormatException nfe) { 46 // If the property cannot be parsed into an int, ignore it. 47 } 48 } 49 high = h; 50 51 cache = new Integer[(high - low) + 1]; //缓存数组大小为256 52 int j = low; //得到最小值-128 53 //从-128开始循环递增把对应的数值加入到缓存数组中 54 for(int k = 0; k < cache.length; k++) 55 cache[k] = new Integer(j++); 56 57 // range [-128, 127] must be interned (JLS7 5.1.7) 58 //校验缓存最大值是否大于127 59 assert IntegerCache.high >= 127; 60 } 61 62 private IntegerCache() {} 63 }
关于hashCode 和 equels:
接口:
抽象类:
单一职责原则(Single Responsibility Priciple):
开闭原则(Open Close Principle):
里式替换原则(Liskov Substitution Principle):
依赖倒置原则(Dependence Inversion Principle):
接口隔离原则(Interface Segregation Principle):
迪米特原则(Law of Demeter or Least Knowlegde Principle):
(以上所有内容皆为个人笔记)
标签:理解 数值 jdk和jre boolean ons sop 包含 一个 字符串
原文地址:https://www.cnblogs.com/xihuantingfeng/p/11604790.html