学习资源:拉勾教育
/* 编程实现People类的封装 */ public class People { // 1.私有化成员变量,使用private关键字修饰 private String name; private int age; private String country; // 3.在构造方法中调用set方法进行合理值的判断 public People() {} public People(String name, int age, String country) { setName(name); setAge(age); setCountry(country); } // 2.提供公有的get和set方法,并在方法体中进行合理值的判断 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { if(age > 0 && age < 150) { this.age = age; } else { System.out.println("年龄不合理!"); } } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public void show() { System.out.println("姓名:" + getName() + " 年龄:" + getAge() + " 国籍:" + getCountry()); } }
/* 编程实现People类的测试 */ public class PeopleTest { public static void main(String[] args) { // 1.使用有参方式构造两个对象并打印 People p1 = new People("张飞",30,"China"); p1.show(); People p2 = new People("关羽",35,"China"); p2.show(); } }
/* 编程实现People类的封装 */ public class People { // 1.私有化成员变量,使用private关键字修饰 private String name; private int age; public static String country; // 3.在构造方法中调用set方法进行合理值的判断 public People() {} public People(String name, int age, String country) { setName(name); setAge(age); setCountry(country); } // 2.提供公有的get和set方法,并在方法体中进行合理值的判断 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { if(age > 0 && age < 150) { this.age = age; } else { System.out.println("年龄不合理!"); } } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public void show() { System.out.println("姓名:" + getName() + " 年龄:" + getAge() + " 国籍:" + getCountry()); } }
/* 编程实现People类的测试 */ public class PeopleTest { public static void main(String[] args) { // 3.验证static关键字修饰的静态成员(类成员)是否与创建对象无关 System.out.println("获取到的国籍信息是:" + People.country); System.out.println("------------------------"); // 1.使用有参方式构造两个对象并打印 People p1 = new People("张飞",30,"China"); p1.show(); People p2 = new People("关羽",35,"China"); p2.show(); System.out.println("------------------------"); // 2.验证static关键字修饰的静态成员(类成员)是否被所有对象共享 p1.country = "蜀国"; System.out.println("第一个对象的国籍是" + p1.country); System.out.println("第二个对象的国籍是" + p2.country); People p3 = new People(); System.out.println("第三个对象的国籍是" + p3.country); } }
/* 编程实现static关键字的使用 */ public class StaticTest { private int cnt = 1; // 隶属于对象层级,也就是每个对象拥有独立的一份 private static int snt = 2; // 隶属于类层级,也就是所有对象都共享同一份 // 自定义非静态的成员方法 需要使用引用.方式访问 public void show() { System.out.println("cnt = " + this.cnt); // 1 System.out.println("snt = " + this.snt); // 2 静态成员被所有对象共享,this关键字可以省略 } // 自定义静态的成员方法,推荐使用类名.的方式访问 public static void test() { //System.out.println("cnt = " + this.cnt); // 1 静态成员方法中没有this关键字,因为是可以勇敢类名.方式调用的 System.out.println("snt = " + this.snt); // 2 } public static void main(String[] args) { StaticTest st = new StaticTest(); st.show(); System.out.println("----------------------"); StaticTest.test(); } }
/* 编程实现构造块和静态代码块的使用 */ public class BlockTest { // 当需要在执行构造方法体前做一些准备工作时,则将准备工作的相关代码写在构造块中即可 // 比如:对成员变量进行的同意初始化操作 { System.out.println("+++构造块"); } // 静态代码块会随着类的加载而准备就绪,会先于构造快执行 // 当需要在执行代码块之前随着类的加载做一些准备工作时,则编写代码到静态代码块中 // 比如:加载数据库的驱动包等 static { System.out.println("===静态代码块"); } // 自定义构造方法 public BlockTest() { System.out.println("---构造方法体"); } public static void main(String[] args) { BlockTest bt = new BlockTest(); BlockTest bt2 = new BlockTest(); } }
public static void main(String[] args)
/* 编程实现main方法的测试 */ public class MainTest { public static void main(String[] args) { System.out.println("参数数组中元素的个数是:" + args.length); System.out.println("传递给main方法的实际参数为:"); for(int i = 0 ; i < args.length ; i++) { System.out.println("下标为" + i + "的形参变量数值为:" + args[i]); } } }
/* 编程实现Singleton类的封装(饿汉式) */ public class Singleton { // 2.声明本类类型的引用指向本类类型的对象 使用private static关键字共同修饰 private static Singleton sin = new Singleton(); // 1.私有化构造方法,使用private关键字修饰 private Singleton() {} // 3.提供公有的get方法,负责将对象返回出去 使用public static关键字共同修饰 public static Singleton getInstance() { return sin; } }
/* 编程实现Singleton类的封装(懒汉式) */ public class Singleton { // 2.声明本类类型的引用指向本类类型的对象 使用private static关键字共同修饰 private static Singleton sin = null; // 1.私有化构造方法,使用private关键字修饰 private Singleton() {} // 3.提供公有的get方法,负责将对象返回出去 使用public static关键字共同修饰 public static Singleton getInstance() { if(null == sin) { sin = new Singleton(); } return sin; } }
/* 编程实现Singleton类的测试 */ public class SingletonTest { public static void main(String[] args) { // 1.声明Singleton类型的引用指向该类型的对象 //Singleton s1 = new Singleton(); //Singleton s2 = new Singleton(); //System.out.println(s1 == s2); // 比较变量s1的数值是否与变量s2的数值相等 false //Singleton.sin = null; 可以使得引用变量无效 Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); System.out.println(s1 == s2); // true } }