Java面向对象设计 – Java访问级别 类简单名称是 class 关键字和 {)之间的名称。 当我们通过简单的名称引用一个类时,编译器在引用类所在的同一个包中查找该类声明。 我们可以使用全名来引用一个类如下。 com.w3cschool.Dog aDog; 指定类的访问级别的一般语法是 <access level modifier>class <class name> { // Body of the class } 类声明中<access level modifier>只有两个有效值: no value public 没有值被称为包级别访问。具有包级别访问的类只能在声明它的包中访问。 具有公共访问级别修改器的类可以从应用程序中的任何包访问。 package com.w3cschool; public class Dog { }
Java设计模式 – 战略模式 在策略模式中,可以在运行时更改算法。 策略模式是一种行为模式。 在策略模式中,我们创建表示各种算法的对象和运行算法的上下文对象。 策略对象更改上下文对象上的算法。 例子 interface MathAlgorithm { public int calculate(int num1, int num2); } class MathAdd implements MathAlgorithm{ @Override public int calculate(int num1, int num2) { return num1 + num2; } } class MathSubstract implements MathAlgorithm{ @Override public int calculate(int num1, int num2) { return num1 - num2; } } class MathMultiply implements MathAlgorithm{ @Override public int calculate(int num1, int num2) { return num1 * num2; } } class MathContext { private MathAlgorithm algorithm; public MathContext(MathAlgorithm strategy){ this.algorithm = strategy; } public int execute(int num1, int num2){ return algorithm.calculate(num1, num2); } } public class Main { public static void main(String[] args) { MathContext context = new MathContext(new MathAdd()); System.out.println("10 + 5 = " + context.execute(10, 5));...
Java设计模式 – 状态模式 在状态模式中,类行为基于其状态而改变。 状态模式是一种行为模式。 当使用状态模式时,我们创建各种状态对象和上下文对象,其行为随着其状态对象改变而变化。 例子 interface State { public void doAction(Context context); } class StartState implements State { public void doAction(Context context) { System.out.println("In start state"); context.setState(this); } public String toString() { return "Start State"; } } class StopState implements State { public void doAction(Context context) { System.out.println("In stop state"); context.setState(this); } public String toString() { return "Stop State"; } } class PlayState implements State { public void doAction(Context context) { System.out.println("In play state"); context.setState(this); } public String toString() { return "Play State"; } } class Context { private State state; public Context() { state = null; } public void setState(State state) { this.state = state; } public State getState() { return state; } } public class Main...
Java设计模式 – 单例模式 单例模式是一种创建模式。 这种模式只涉及一个单独的类,它负责创建自己的对象。 该类确保只创建单个对象。 这个类提供了一种访问其唯一对象的方法。 例如,当设计一个用户界面时,我们可能只有一个主应用程序窗口。我们可以使用Singleton模式来确保只有一个MainApplicationWindow对象的实例。 例子 下面的代码将创建一个MainWindow类。 MainWindow类的构造函数是私有的,并且有一个自身的静态实例。 MainWindow类提供了一个静态方法来获取它的静态实例到外部世界。 Main,我们的演示类将使用MainWindow类来获取一个MainWindow对象。 class MainWindow { //create an object of MainWindow private static MainWindow instance = new MainWindow(); //make the constructor private so that this class cannot be //instantiated by other class private MainWindow(){} //Get the only object available public static MainWindow getInstance(){ return instance; } public void showMessage(){ System.out.println("Hello World!"); } } public class Main { public static void main(String[] args) { //Get the only object available MainWindow object = MainWindow.getInstance(); //show the message object.showMessage(); } } 上面的代码生成以下结果。