p37:
练习1
/** * Created by xkfx on 2017/2/22. */ public class DataOnly { int anInt; char aChar; public static void main(String[] args) { DataOnly dateOnly = new DataOnly(); // 静态方法中无法直接打印成员数据,需要先new一个对象 System.out.println(dateOnly.anInt + " " + dateOnly.aChar); } }
练习2
/** * Created by xkfx on 2017/2/22. */ public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); // System类位于默认导入的java.lang包中,out是该类的静态成员,可以通过类名直接使用。 } }
练习3
/** * Created by xkfx on 2017/2/22. */ public class ATypeName { public static void main(String[] args) { ATypeName a = new ATypeName(); // 这个内容在书的第25页 } }
练习4、练习5
class DataOnly { int i; double d; boolean b; DataOnly() { this.i = 47; this.d = 1.1; this.b = false; } public static void main(String[] args) { DataOnly dataOnly = new DataOnly(); System.out.println(dataOnly.i + " " + dataOnly.d + " " + dataOnly.b); } }
练习6
/** * Created by xkfx on 2017/2/22. */ public class TestStorage { static int storage(String s) { return s.length() * 2; } public static void main(String[] args) { int length = TestStorage.storage("Not a hello world."); System.out.println(length); } }
练习7
/** * Created by xkfx on 2017/2/22. */ class Incrementable { static void increment() { StaticTest.i++; } } class StaticTest { static int i = 47; public static void main(String[] args) { Incrementable.increment(); System.out.println(i); } }