java之static的应用
对于java中static关键字的介绍,我想对于有一定的java基础的博友应该都知晓!它主要用于修饰变量,和方法,比如常用的类成员和类方法,就是采用static进行修饰的!本博文我不想去深入探讨static的作用,而主要想介绍一下两个案例:模拟数据库表主键id的自增,静态工厂方法!
首先介绍第一个:模拟数据库表主键id的自增。很自然的,我们会建立一个class,包含两个成员,一个是id,用于模拟数据库表的主键;另一个是nextId,用于记录自增长的那个变量,它是static的;最后是一个getNextId()获取当前id以及让nextId自增的方法。要达到的效果:没创建一个类对象,该对象相对于上一个创建的对象的id要加1。
下面贴出代码:
<span style="font-size:18px;">package com.steadyjack.chapter4;/*** @author 钟林森* static 实现静态id 自增长*/
public class A {private static int nextId;private int id=getNextId();private int getNextId(){int r=nextId;nextId++;return r;}public void showId(){System.out.println("当前id: "+id);}public static void main(String[] args) {A a=new A();a.showId();A b=new A();b.showId();A c=new A();c.showId();A d=new A();d.showId();}
}
</span>
接下来是static的应用之二:静态工厂方法
工厂方法模式(Factory Method):定义一个用于创建对象的类(或者接口),在该类(或者接口的实现类)中的具体方法根据传进的参数即具体的子类决定实例化哪一个子类,该模式使得一个类的实例化延迟到其子类。
静态工厂方法:是一种比较简单的工厂模式--就是在类的方法产生各种需要的类对象,并采用类.方法直接调用返回需要的对象!
下面是模拟代码:
<span style="font-size:18px;">package com.steadyjack.chapter4;/*** @author 钟林森* 静态工厂方法*/
public class FactoryMethod {public static void main(String[] args) {Apple appleApple();apple.show();Orange orangeOrange();orange.show();}
}class IFactoryFruit {public static Apple getApple(){return new Apple("苹果",23.5);}public static Orange getOrange(){return new Orange("橘子", 35.5);}
}class IFruit {private String name;private Double weight;public IFruit(String name, Double weight) {super();this.name = name;this.weight = weight;}public IFruit() {super();}public void show(){System.out.println("我叫: "+name+" -- 体重: "+weight);}
}class Apple extends IFruit{public Apple(String name, Double weight) {super(name, weight);}}class Orange extends IFruit{public Orange(String name, Double weight) {super(name, weight);}}
</span>
<span style="font-size:18px;">package com.steadyjack.chapter4;/*** @author 钟林森* 面向接口 工厂方法模式*/
public class StaticFactoryMethod {public static void main(String[] args) {AnimalFactoryMethod animalInstance=new AnimalFactoryMethod();//泛型IAnimal tiger1AnimalInstance(Tiger.class);tiger1.show();IAnimal lion1AnimalInstance(Lion.class);lion1.show();//面向接口Tiger tiger2=(Tiger) Animal(new Tiger());tiger2.show();Lion lion2=(Lion) Animal(new Lion());lion2.show();}
}class AnimalFactoryMethod{//泛型(泛型方法)和反射@SuppressWarnings("unchecked")public <T extends IAnimal> T getAnimalInstance(Class<T> c){T animal=null;try {animal=(T) Class.Name()).newInstance();} catch (Exception e) {e.printStackTrace();}return animal;}public IAnimal getAnimal(IAnimal animal){if (animal instanceof Tiger) {return new Tiger();}else if (animal instanceof Lion) {return new Lion();}return null;}
}interface IAnimal{void show();
}class Tiger implements IAnimal{@Overridepublic void show() {System.out.println("我是一只tiger");}
}class Lion implements IAnimal{@Overridepublic void show() {System.out.println("我是一只lion");}
}</span>
本文发布于:2024-02-04 13:42:19,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170708542056078.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |