0
点赞
收藏
分享

微信扫一扫

static,final,单例设计模式和abstract(保姆级笔记)

言诗把酒 2022-01-08 阅读 85

 

目录

Static修饰符

Static变量

静态方法和非静态方法

静态代码块

匿名代码块

         单例设计模式

单例的饿汉式实现 

 单例的懒汉式实现:

视频笔记——饿汉式与懒汉式的对比: 

final修饰符

修饰成员变量-非静态成员变量 :

 修饰成员变量-静态成员变量:

final修饰引用变量

面试题

抽象类

模板方法设计模式的相关笔记


Static修饰符

Static变量

 下面看一段代码

public class student {
	int age;
	static String name;
	public static void main(String[] args)
	{
  student a1=new student();
  student a2=new student();
  a1.age=10;
  a1.name="小明";
  a2.age=20;
  System.out.println(a1.age);
  System.out.println(a2.age);
  System.out.println(a1.name);
  System.out.println(a2.name);
		
}
}

public class student {
	int age;
	static String name;
	public static void main(String[] args)
	{
  student a1=new student();
  student a2=new student();
  a1.age=10;
  a1.name="小明";
  a2.age=20;
  a2.name="小王";
  System.out.println(a1.age);
  System.out.println(a2.age);
  System.out.println(a1.name);
  System.out.println(a2.name);
		
}
}

 

 当为a2.name赋值时,a1.name也随之改变

静态方法和非静态方法

public class student {
	int age;
	static String name;
	public void a() {}
	public static void b() {}
	public static void test()
	{
		System.out.println(age);//编译报错
	}
}

 

 若改为name则编译通过

当把static去掉,为非静态方法时

父类的静态方法可以被子类继承,但是不能被子类重写

public class student {
	int age;
	public static void b() {}
}
public class studentA extends student//继承父类
{
public void b()//编译报错
{
	System.out.println(this.age);
}
}

 继承但不重写的时候

public static void main(String[] args)
{
studentA A=new studentA();
A.b();
} 	

编译通过,但不是重写 


public class student {
	int age;
	public  void b() {}
}
public class studentA extends student//继承父类
{
	public static void b();//编译报错
}

静态代码块

static{

}
public class student {
	public static int age;
	static
	{ 
		age=1;
	}
	public  void b() {
		age=2;
	}
	public static void main(String[] args)
	{
     System.out.println(student.age);
    }
}

匿名代码块

public class s{
{
 //匿名代码

}
}
public class student {
private int age;
	public student()
	{
		System.out.println("student构造器");
		test();
	}
	public void test()
	{
		System.out.println("student  test方法:age="+age);
	}
}
public class studentA extends student//继承父类
{
private int age=10;
	{
		System.out.println("子类匿名代码块");
	}
	static {
		System.out.println("子类静态代码块");
	}
	public studentA()
	{
		System.out.println("子类构造器");
	}
	public void test()
	{
		System.out.println("子类studentA test方法:age="+age);
	}
public static void main(String[] args)
{
	new studentA();
} 	
}

视频笔记: 

         单例设计模式

单例的饿汉式实现 

public class demo{
	public static void main(String[] agrs)
	{
		bink b=bink.test();
		bink c=bink.test();
		System.out.println(b==c);
	}
}
 class bink{
//私有化构造器
	private bink() {
	}
//内部创建类的对象,要求此对象必须也是静态的
	private static bink a=new bink();
//提供公共的静态方法,返回类的对象
	public static bink test()
	{
		return a;
	}
}

 单例的懒汉式实现:

public class demo{
	public static void main(String[] agrs)
	{
		bink b=bink.test();
		bink c=bink.test();
		System.out.println(b==c);
	}
}
 class bink{
//私有化构造器
	private bink() {
	}
//声明当前类对象,没有初始化,此对象必须也声明为静态的
	private static bink a=null;
//声明public,static的返回当前类对象的方法
	public static bink test()
	{
		if(a==null)
		{
		a=new bink();
		}
		return a;
	}
}

视频笔记——饿汉式与懒汉式的对比: 

视频笔记——属性赋值的先后顺序

final修饰符

 

 编译报错

编译报错

当对a进行2次赋值时编译报错

public class Person{
 private final int a;
}

视频笔记: 

public class Person{ 
private static final int a; 
}

final修饰引用变量

public  class student {
String name;
public student()
{
	
}
public void set(String name)
{
	this.name=name;
}
public static void main(String[] args)
{
	final student a1=new student();
a1.set("xx");
a1.set("yy");
}
}

编译通过

new一次以后

编译报错

面试题

1.改错

public class s{
public int add(final x)
{
return ++x;
}
}

编译报错,应改为

public class s{
public int add(final x)
{
return x+1;
}
}

2.

public class some{
public static void main(String[] args)
{
other o=new other();
new some().add(o);
}
public void add(final other o)
{//o=new other();
o.i++
}
class other{
public int i;
}

编译通过,o是常量,但是i是变量,所以是没问题的

抽象类

public abstract class parent{
abstract void test();//定义抽象方法
}
public  class student {
String name;
public student()
{	
}
public static void main(String[] agrs)
{
st s1=new st(1);//编译失败
}
}
abstract class st{
	int age;
	public st(int age)
	{
		this.age=age;
	}
}

 编译报错

当把abstract去掉后则正常

public  abstract class student {
String name;
public abstract void test();
 class st extends student{
	public void test()
	{
		
	}
 }
 

编译通过

代码举例

public abstract class employee {
private String name;
private int id;
private double salary;
public employee()
{
	super();
}
public employee(String name,int id,double salary)
{
	this.name=name;
	this.id=id;
	this.salary=salary;
}
	public abstract void work();//抽象方法
}
public class manger extends employee {
	private double bonus;
	public manger(double bonus)
	{
		super();
		this.bonus=bonus;
	}
	public manger(String name,int id,double salary,double bonus)
	{
		super(name,id,salary);
		this.bonus=bonus;
	}
	public void work()
	{
		System.out.println("管理员工");
	}

}
public class commonemployee extends employee {
	public void work()
	{
		System.out.println("员工在一线车间生产产品");
	}

}
public class employeetest {
public static void main(String[] args)
{
	manger a1=new manger("小王",1000,4000,10000);
	a1.work();
	commonemployee b1=new commonemployee();
   b1.work();
}
}

 

模板方法设计模式的相关笔记

public class timetest {
	public static void main(String[] args)
	{
		st M=new st();
		M.spendtime();
	}
}
abstract class template{
	public void spendtime()
	{//计算某段代码执行所需要花费的时间
		long start =System.currentTimeMillis();
		code();//不确定的部分,易变的部分,
		long end =System.currentTimeMillis();
		System.out.println("花费的时间为:"+(end-start));
		
	}
	public abstract void code();
}
class st extends template{
	public void code()
	{
		for(int i=2;i<=1000;i++)
		{
			boolean a=true;
			for(int j=2;j<=Math.sqrt(i);j++)
			{
				if(i%j==0)
					a=false;
				break;
			}
			if(a)
			{
				System.out.println(i);
			}
		}
		
	}
}

举报

相关推荐

0 条评论