java新手笔记11 类的静态属性、方法(单例)

1、Person类

package com.yfs.javase;



public class Person {

	

	String name;//每个对象上分配 与对象绑定

	int age;

	char sex;

	boolean married;

	

	static String country = "中国";//静态属性  与类绑定 所有对象共享

	

	public static void baseInfo () {//静态方法  与类绑定 所有对象共享

		System.out.println("舌尖上的中国...");

		//method() ;//静态方法不能调用非静态方法

		//String n = name;//静态方法不能访问非静态属性

		String n = country;

	}

	

	public void test () {//非静态方法

		System.out.println("call test() ....");

		name = "Tom";//访问非静态属性

		String  s = country;//访问静态属性

		System.out.println("s = " + s);

		//调用静态方法

		baseInfo();

	}

	

	public void method () {

		System.out.println("method() ....");

	}



}

 2.Person测试1

package com.yfs.javase;



public class PersonTest {



	public static void main(String[] args) {

		Person p1 = null;

		p1 = new Person();

		p1.name = "张三";

		

		Person p2 = new Person();

		p2.name = "李四";

		System.out.println("p1.name  " + p1.name);

		System.out.println("p2.name  " + p2.name);

		System.out.println("=========================");

		p1.name = "王五";

		

		System.out.println("p1.name  " + p1.name);

		System.out.println("p2.name  " + p2.name);

		

		//静态属性通过类名访问

		System.out.println("Person.country  " + Person.country);

		

        p1.country = "日本";

        System.out.println("Person.country  " + Person.country);

        System.out.println("p2.country  " + p2.country);

        

        

//        Person.baseInfo();//静态属性 方法通过类访问

//        p1.baseInfo();

//        p2.baseInfo();

          p1.test();

	}



}

 3.测试Person2

package com.yfs.javase;



public class PersonTest1 {



	public static void main(String[] args) {

		Person p1 = null;//类加载

		System.out.println(Person.country);

		Person.baseInfo();

		//p1.name = "张三";

		

	}



}

 4.Person 类2(代码块、静态代码块)

package com.yfs.javase;



public class Person {



	String name;// 每个对象上分配 与对象绑定

	int age;

	char sex;

	boolean married;

	// 代码块 执行时机 创建对象时执行

	{

		System.out.println("这是代码块的内容");

	}

	// 静态代码块  与类绑定 只执行一次

	static {

          System.out.println("这是静态代码块内容....");

	}



	public Person() {

		System.out.println("创建Person对象");

	}



	public Person(String name) {

		System.out.println("Person(name) ....");

		this.name = name;

	}



	public Person(String name, int age, char sex, boolean married) {

		// this(name);//构造方法调用

		this.name = name;

		this.age = age;

		this.sex = sex;

		this.married = married;

		System.out.println("四个参数的构造方法...");

	}



	static String country = "中国";// 静态属性 与类绑定 所有对象共享



	public static void baseInfo() {// 静态方法 与类绑定 所有对象共享

		System.out.println("舌尖上的中国...");

		String n = country;

	}



	public void test() {// 非静态方法

		System.out.println("call test() ....");

		name = "Tom";// 访问非静态属性

		String s = country;// 访问静态属性

		System.out.println("s = " + s);

		// 调用静态方法

		baseInfo();

	}



	public void method() {

		System.out.println("method() ....");

	}



}

 5.Person类 2  测试

package com.yfs.javase;



public class PersonTest {



	public static void main(String[] args) {

		Person p1 = new Person("张三",20,'男',false);

		

		Person p2 = new Person();

		

		Person p3 = new Person("李四");

	}



}

 6.Person类3 (private 属性、方法、get/set方法)

package com.yfs.javase;



public class Person {

	// 属性私有private

	private String name = "Jack";// 每个对象上分配 与对象绑定

	private int age;

	private char sex;

	private boolean married;

	static String country = "中国";// 静态属性 与类绑定 所有对象共享

	//查看对象的属性值

	public String toString() {

		return "name = " + name + ", age = " + age + ", sex = " + sex + ", married = " + married;

	}

  

	{

		name = "Tom";

	}

	// 设置方法 参数

	// public void setAge(int age) {

	// if (age > 150) {

	// System.out.println("年龄不合法");

	// return;

	// }

	// this.age = age;

	// }

	//

	// // 获取的方法

	// public int getAge() {

	// return age;

	// }

	// 通过get set方法 设置 获取属性的值

	public Person() {

		System.out.println("创建Person对象");

	}



	public String getName() {

		return name;

	}



	public void setName(String name) {

		this.name = name;

	}



	public int getAge() {

		return age;

	}



	public void setAge(int age) {

		this.age = age;

	}



	public char getSex() {

		return sex;

	}



	public void setSex(char sex) {

		this.sex = sex;

	}



	public boolean isMarried() {

		return married;

	}



	public void setMarried(boolean married) {

		this.married = married;

	}



	public Person(String name) {

		this.name = name;

	}



	public Person(String name, int age, char sex, boolean married) {

		this.name = name;

		this.age = age;

		this.sex = sex;

		this.married = married;

	}



	public static void baseInfo() {// 静态方法 与类绑定 所有对象共享

		System.out.println("舌尖上的中国...");

		//method();静态方法不能访问

	}



	public void test() {// 非静态方法

		System.out.println("call test() ....");

		method();//在同一类中私有属性 方法可以调用

	}



	private void method() {//私有方法   范围在类的内部访问

		System.out.println("method() ....");

		//test();

		baseInfo();

		String n = name;

	}



}

 7.Person类3测试

package com.yfs.javase;



public class PersonTest {



	public static void main(String[] args) {

		Person p1 = new Person("张三",20,'男',false);

		System.out.println(p1.toString());

		

//		System.out.println("p1.name = " + p1.name);

//		System.out.println("p1.age = " + p1.age);

//		System.out.println("p1.sex = " + p1.sex);

//		System.out.println("p1.married = " + p1.married);

		

		//p1.name = "张飞";

		//修改年龄  通过方法

		//p1.age = 2000;

		p1.setAge(2000);

		p1.setAge(28);

		p1.setName("张XX");

		

		System.out.println("p1.getName() = " + p1.getName());

		System.out.println("p1.getAge() = " + p1.getAge());

		System.out.println("p1状态:  " + p1.toString());

		

		p1 = new Person("张三",21,'男',false);

		//p1.method();//私有方法不能访问

		

	}



}

 8.单例对象 模型

package com.yfs.javase;



public class Singleton {

	

	private Singleton () {

		

	}

	//通过一个对象 声明static 

	public static Singleton getInstance() {

		Singleton s = new Singleton();

		return s;

	}



	public void eat() {

		System.out.println("eat().....");

	}

}

 9.单例对象测试

package com.yfs.javase;



public class Test {



	public static void main(String[] args) {

		//Singleton sing = new Singleton();

		Singleton sing = Singleton.getInstance();

		sing.eat();

	}



}

 

你可能感兴趣的:(java)