源码:
public class ClassAndObjectTest { public static void main(String[] args) { //创建类的实例,定义一个对象变量引用这一实例 MyClass obj = new MyClass(); //通过对象变量调用类的公有方法 obj.myMethod("Hello"); //给属性赋值 obj.setValue(100); //输出属性的当前值 System.out.println(obj.getValue()); //直接访问对象公有字段 obj.Information = "Information"; //输出对象公有字段的当前值 System.out.println(obj.Information); } } /** * 自定义Java类的示例 */ class MyClass { // 公有字段 public String Information = ""; // 自定义公有Java实例方法 public void myMethod(String argu) { System.out.println(argu); } // 定义属性:私有字段+get方法+set方法 private int value; public int getValue() { return value; } public void setValue(int value) { this.value = value; } }
实验结果:
源码与结果:
public class Hello { public static void main(String[] args) { System.out.println("Hello World!"); } }
public class InitializeBlockDemo { /** * @param args */ public static void main(String[] args) { InitializeBlockClass obj=new InitializeBlockClass(); System.out.println(obj.field); obj=new InitializeBlockClass(300); System.out.println(obj.field); } } class InitializeBlockClass{ //下面这句在初始化块之前与之后,会影响到field字段的初始值 //public int field=100; { field=200; } public int field=100; public InitializeBlockClass(int value){ this.field=value; } public InitializeBlockClass(){ } }
//字符串反转 public class MyPackageClass { public static String reverseString(String inputStr) { StringBuffer buff=new StringBuffer(); for(int i=inputStr.length()-1;i>=0;i--) buff.append(inputStr.charAt(i)); return buff.toString(); } }
public class Test1 { public static void main(String args[]) { String str="abcd"; StringBuffer buff=new StringBuffer(); for(int i=str.length()-1;i>=0;i--) buff.append(str.charAt(i)); System.out.println(buff.toString()); } }
public class ObjectEquals { public static void main(String[] args) { MyTestClass obj1=new MyTestClass(100); MyTestClass obj2=new MyTestClass(100); System.out.println(obj1==obj2); System.out.println(obj1.equals(obj2)); } } class MyTestClass { public int Value; //注意:只有参数类型为Object的,才是重写了Object的equals方法 //参数类型为MyTestClass的,仅仅是Overload了equals方法。 // @Override // public boolean equals(Object obj) // { // return ((MyTestClass)obj).Value==this.Value; // } public boolean equals(MyTestClass obj) { return obj.Value==this.Value; } public MyTestClass(int initValue) { Value=initValue; } }
public class StrangeIntegerBehavior { public static void main(String[] args) { Integer i1=100; Integer j1=100; System.out.println(i1==j1); Integer i2=129; Integer j2=129; System.out.println(i2==j2); } }
class Root { static{ System.out.println("Root的静态初始化块"); } { System.out.println("Root的普通初始化块"); } public Root() { System.out.println("Root的无参数的构造器"); } } class Mid extends Root { static{ System.out.println("Mid的静态初始化块"); } { System.out.println("Mid的普通初始化块"); } public Mid() { System.out.println("Mid的无参数的构造器"); } public Mid(String msg) { //通过this调用同一类中重载的构造器 this(); System.out.println("Mid的带参数构造器,其参数值:" + msg); } } class Leaf extends Mid { static{ System.out.println("Leaf的静态初始化块"); } { System.out.println("Leaf的普通初始化块"); } public Leaf() { //通过super调用父类中有一个字符串参数的构造器 super("Java初始化顺序演示"); System.out.println("执行Leaf的构造器"); } } public class TestStaticInitializeBlock { public static void main(String[] args) { new Leaf(); } }