目录
范例1-2:定义一个com.yootk.util.Message的类。
范例1-3:定义一个com.yootk.test.TestMessage的类,这个类要使用Message类。
范例1-10:定义com.yootk.demob.B类,此类继承A类。
第一章 包及访问控制权限
1.1 包的定义
范例1-1:定义包。
package com.yootk.demo ; // 定义程序所在包,此语句必须放在首行 public class Hello { public static void main(String args[]) { System.out.println("Hello World !") ; } } |
1.2 包的导入
范例1-2:定义一个com.yootk.util.Message的类。
package com.yootk.util; public class Message { public void print() { System.out.println("Hello World !"); } } |
范例1-3:定义一个com.yootk.test.TestMessage的类,这个类要使用Message类。
package com.yootk.test; import com.yootk.util.Message; // 导入所需要的类 public class TestMessage { public static void main(String args[]) { Message msg = new Message(); // 实例化对象 msg.print(); // 调用方法 } } |
范例1-4:修改Message类定义。
package com.yootk.util; class Message { // 此处没有使用public class定义 public void print() { System.out.println("Hello World !"); } } |
范例1-5:导入一个包中的多个类。
package com.yootk.test; import com.yootk.util.*; // 自动导入指定包中所需要的类 public class TestMessage { public static void main(String args[]) { Message msg = new Message(); // 实例化对象 msg.print(); // 调用方法 } } |
范例1-6:增加一个新的Message类。
package org.lxh; public class Message { public void get() { System.out.println("世界,你好!"); } } |
范例1-7:导入程序包。
package com.yootk.test; import org.lxh.*; // 包中存在Message类 import com.yootk.util.*; // 包中存在Message类 public class TestMessage { public static void main(String args[]) { Message msg = new Message(); // 实例化对象,出现错误 msg.print(); // 调用方法 } } |
1.3 常见系统包
范例1-8:定义一个Message.java文件。
package com.yootk.util; public class Message { public void print() { System.out.println("Hello World !"); } } |
1.5 访问控制权限
范例1-9:定义com.yootk.demoa.A类。
package com.yootk.demoa ; public class A { protected String info = "Hello" ; // 使用protected权限定义 } |
范例1-10:定义com.yootk.demob.B类,此类继承A类。
package com.yootk.demob; import com.yootk.demoa.A; public class B extends A { // 是A不同包的子类 public void print() { // 直接访问父类中的protected属性 System.out.println("A类的info = " + super.info); } } |
范例1-11:代码测试。
package com.yootk.test; import com.yootk.demob.B; public class Test { public static void main(String args[]) { new B().print(); } } |
范例1-12:错误的访问。
package com.yootk.test; import com.yootk.demoa.A; public class Test { public static void main(String args[]) { A a = new A(); System.out.println(a.info); // 错误:无法访问 } } |
1.7 单例设计模式(Singleton)
范例1-13:构造方法非私有化。
class Singleton { // 定义一个类,此类默认提供无参构造方法 public void print() { System.out.println("Hello World ."); } } public class TestDemo { public static void main(String args[]) { Singleton inst = null; // 声明对象 inst = new Singleton(); // 实例化对象 inst.print(); // 调用方法 } } |
范例1-14:私有化构造方法。
class Singleton { // 定义一个类 private Singleton() { // 构造方法私有化 } public void print() { System.out.println("Hello World ."); } } public class TestDemo { public static void main(String args[]) { Singleton inst = null; // 声明对象 inst = new Singleton(); // 错误:The constructor Singleton() is not visible inst.print(); // 调用方法 } } |
范例1-15:第一步思考。
class Singleton { // 定义一个类 Singleton instance = new Singleton() ; // 在内部实例化本类对象 private Singleton() { // 构造方法私有化 } public void print() { System.out.println("Hello World ."); } } |
范例1-16:第二步思考。
class Singleton { // 定义一个类 static Singleton instance = new Singleton() ; // 可以由类名称直接访问 private Singleton() { // 构造方法私有化 } public void print() { System.out.println("Hello World ."); } } public class TestDemo { public static void main(String args[]) { Singleton inst = null; // 声明对象 inst = Singleton.instance; // 利用“类.static属性”方式取得实例化对象 inst.print(); // 调用方法 } } |
范例1-17:第三步思考。
class Singleton { // 定义一个类 private static Singleton instance = new Singleton() ; private Singleton() { // 构造方法私有化 } public void print() { System.out.println("Hello World ."); } public static Singleton getInstance() { // 取得本类对象 return instance; } } public class TestDemo { public static void main(String args[]) { Singleton inst = null; // 声明对象 inst = Singleton.getInstance(); // 利用“类.static方法()”取得实例化对象 inst.print(); // 调用方法 } } |
范例1-18:程序出现的问题。
class Singleton { // 定义一个类 private static Singleton instance = new Singleton() ; private Singleton() { // 构造方法私有化 } public void print() { System.out.println("Hello World ."); } public static Singleton getInstance() { // 取得本类对象 instance = new Singleton(); // 重新实例化对象 return instance; } } |
范例1-19:解决程序的问题。
class Singleton { // 定义一个类 private final static Singleton instance = new Singleton() ; private Singleton() { // 构造方法私有化 } public void print() { System.out.println("Hello World ."); } public static Singleton getInstance() { // 取得本类对象 return instance; } } public class TestDemo { public static void main(String args[]) { Singleton inst = null; // 声明对象 inst = Singleton.getInstance(); // 利用“类.static方法()”取得实例化对象 inst.print(); // 调用方法 } } |
1.8 多例设计模式
范例1-20:定义一个表示性别的类。
package com.yootk.demo; class Sex { private String title; private static final Sex MALE = new Sex("男"); private static final Sex FEMALE = new Sex("女"); private Sex(String title) { // 构造私有化了 this.title = title; } public String toString() { return this.title; } public static Sex getInstance(int ch) { // 返回实例化对象 switch (ch) { case 1: return MALE; case 2: return FEMALE; default: return null; } } } public class TestDemo { public static void main(String args[]) { Sex sex = Sex.getInstance(2); System.out.println(sex); } } |
范例1-21:利用接口标记对象内容。
interface Choose { public int MAN = 1; // 描述数字 public int WOMAN = 2; // 描述数字 } public class TestDemo { public static void main(String args[]) { Sex sex = Sex.getInstance(Choose.MAN) ; // 利用接口标记内容取得对象 System.out.println(sex) ; } } |
范例1-22:对取得Sex类对象进行修改。
package com.yootk.demo; class Sex { private String title; private static final Sex MALE = new Sex("男"); private static final Sex FEMALE = new Sex("女"); private Sex(String title) { // 构造私有化了 this.title = title; } public String toString() { return this.title; } public static Sex getInstance(String ch) { switch (ch) { // 利用字符串判断 case "man": return MALE; case "woman": return FEMALE; default: return null; } } } public class TestDemo { public static void main(String args[]) { Sex sex = Sex.getInstance("man"); System.out.println(sex); } } |