This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Scope { | |
| int x; //x为实例变量 | |
| int y; //y为实例变量 | |
| static int z; //z为静态变量 | |
| /*局部变量只有在需要使用它的时候定义它 | |
| * 就是将局部变量的作用域最小化*/ | |
| void method(int x){ //x为方法参数 | |
| int y = 1; //y为局部变量 | |
| int z = 1; //z为局部变量 | |
| this.x = x+1; //this.x代表实例变量x,单独的x代表局部变量x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.SimpleTimeZone; | |
| public class Sample1 { | |
| int var1 = 1; //var1是实例变量 | |
| static int var2; //var2是静态变量, | |
| public int add(){ | |
| int var3 = var1+var2; //var3是局部变量 | |
| return var3; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Doll1 { | |
| private static int number = 0; //静态变量 | |
| private int id=0; //实例变量 | |
| private String name; //实例变量 | |
| public Doll1(String name){ //构造方法 | |
| this.name = name; | |
| number ++; | |
| id = number; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface InterfaceDemo { | |
| //public的接口也要和文件名同名 | |
| //定义接口 | |
| //默认的权限为public static final | |
| int i= 8; | |
| void method1(); | |
| void method2(int j); | |
| } | |
| //接口的实现类,不关心接口是怎样调用的 | |
| class ImpInter implements InterfaceDemo{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class AbstractDemo { | |
| public static void main(String[] args){ | |
| //此处进行 | |
| Student1 stu = new Student1("zhangsan",28); | |
| XiDianManager xidian = new XiDianManager(); | |
| xidian.addStudent1(stu); | |
| xidian.deleteStudent1(stu); | |
| } | |
| } | |
| class Student1{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class AutoBoxDemo4 { | |
| public static void main(String[] args){ | |
| Integer i1 = 200; | |
| Integer i2 = 200; | |
| if (i1.equals(i2))//如果用算数运算符 ,会由于integer的缓存机制,导致输出为false | |
| System.out.println("i1==i2"); | |
| else | |
| System.out.println("i1 !== i2"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class WrapperDemo { | |
| public static void main(String[] args){ | |
| int date1 = 10; | |
| int date2 = 20; | |
| //使用integer来打包int数据,integer是整型的意思 | |
| Integer date1Wrapper = new Integer(date1); | |
| Integer date2Wrapper = new Integer(date2); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class AutoBoxDemo { | |
| public static void main(String[] args){ | |
| Integer date1 = 10; | |
| Integer date2 = 20; | |
| //转化为double值再除以3 | |
| System.out.println(date1.doubleValue()/3); | |
| //进行两个值 | |
| System.out.println(date1.compareTo(date2)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class AutoBoxDemo2 { | |
| public static void main(String[] args){ | |
| Integer i1 = 100; | |
| Integer i2 = 100; | |
| if (i1 == i2) | |
| System.out.println("i1==i2"); | |
| else | |
| System.out.println("i1!==i2"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class AutoBoxDemo3 { | |
| public static void main(String[] args){ | |
| Integer i1 = 127; | |
| Integer i2 = 127; | |
| //注意自动装箱的缓存 | |
| if (i1==i2) | |
| System.out.println("i1==i2"); | |
| else | |
| System.out.println("i1!==i2"); | |
| } |