目录
一、单例模式
单例模式-饿汉式
1)将构造器私有化
2)在类的内部直接创建对象(该对象是statis)
3)提供一个公共的static方法,返会对象
/**
* @author: 程序员飞扬
* @description:
*/
public class SingleTon01 {
public static void main(String[] args) {
GirlFriend girlFriend1 = GirlFriend.getInstance();
System.out.println(girlFriend1);
GirlFriend girlFriend2 = GirlFriend.getInstance();
System.out.println(girlFriend2);
//演示为什么是饿汉式:下方代码只是使用了属性,构造器执行了,说明确实创建了对象
System.out.println(GirlFriend.age);
}
}
class GirlFriend{
private String name;
public static int age=10;
private static GirlFriend gf = new GirlFriend("小红");
private GirlFriend(String name) {
System.out.println("构造器执行了");
this.name = name;
}
public static GirlFriend getInstance(){
return gf;
}
@Override
public String toString() {
return "GirlFriend{" +
"name='" + name + '\'' +
'}';
}
}
输出:
构造器执行了
GirlFriend{name='小红'}
GirlFriend{name='小红'}
10
为什么成为饿汉式?
因为该类一被加载,就会创建该对象
单例模式-懒汉式
/**
* @author: 程序员飞扬
* @description:单例模式-饱汉式
*/
public class SingleTon02 {
public static void main(String[] args) {
//System.out.println(Cat.age);
System.out.println(Cat.getInstance());
System.out.println(Cat.getInstance());
}
}
class Cat{
private String name;
public static int age =10;
private static Cat cat;
private Cat(String name) {
System.out.println("构造器加载了");
this.name = name;
}
public static Cat getInstance(){
if(cat == null){
cat = new Cat("花花");
}
return cat;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
'}';
}
}
输出:
构造器加载了
Cat{name='花花'}
Cat{name='花花'}
比较:
二、final关键字
/**
* @author: 程序员飞扬
* @description:
*/
public class Final01 {
public static void main(String[] args) {
C c = new C();
System.out.println(c.salsy = 2.0);
}
}
//当不希望类被继承时
//final class A01{
//}
//class A001{
// //当不希望父类的方法被重写时
// public final void hello(){
//
// }
//}
class A001{
public final double salay = 1.5;
}
class B extends A001 {
}
class C{
//当不希望属性被修改时
public final double salsy = 1.5;
}
class D{
public void hello(){
//当不希望局部变量被修改时
final double a = 0.5;
a = 1.0;
System.out.println(a);
}
}
final注意事项
第2)点说明:
/**
* @author: 程序员飞扬
* @description:
*/
public class FinallyDetail01 {
}
class AA{
public final double TAX_RATE = 0.09;
public final double TAX_RATE2;
public final double TAX_RATE3;
public AA(){
TAX_RATE2 = 0.08;
}
{
TAX_RATE3 = 0.07;
}
}
第3)点说明
/**
* @author: 程序员飞扬
* @description:
*/
public class FinallyDetail02 {
}
class AAA{
public final static double TAX_RATE = 0.09;
public final static double TAX_RATE3;
static{
TAX_RATE3 = 0.07;
}
}
第4)点说明
/**
* @author: 程序员飞扬
* @description:
*/
public class FinallyDetail03 {
A02 a02 = new A02();
}
final class A02{
public double TAX_RATE = 0.09;
}
第5)点说明
/**
* @author: 程序员飞扬
* @description:
*/
public class FinallyDetail04 {
}
class A03{
public double TAX_RATE = 0.09;
public final void hello(){
System.out.println("hello");
}
}
class B03 extends A03{
}
另外几点需要注意的:
第7)点说明:
/**
* @author: 程序员飞扬
* @description:
*/
public class FinallyDetail05 {
public static void main(String[] args) {
System.out.println(A05.a);
}
}
class A05 {
public final static int a = 5;
static{
System.out.println("静态代码快被执行了");
}
}
输出:
5