1、构造器
/**
* test -- 构造器
*/
public class test {
public static void main(String[] args) {
// 成员变量初始化顺序
// a.默认构造器初始化
Person p1 = new Person();
System.out.println(p1.getName() + ":" + p1.getAge());
// b.成员变量显示初始化
Person p2 = new Person("cc");
System.out.println(p2.getName() + ":" + p2.getAge());
Person p3 = new Person(10);
System.out.println(p3.getName() + ":" + p3.getAge());
// c.通过构造器给成员变量初始化
Person p4 = new Person("cc",10);
System.out.println(p4.getName() + ":" + p4.getAge());
// d.通过set方法初始化成员变量
Person p5 = new Person();
p5.setName("dd");
p5.setAge(22);
System.out.println(p5.getName() + ":" + p5.getAge());
}
};
class Person {
private String name = "aa";
private int age = 1;
// 构造器Person(变量) 作用
// 1、构成器用于创建对象
Person() {
System.out.println("空参构造器");
}
// 2、构造器用于成员变量初始化
// 3、构造器之间可以重载
Person(String n, int a){
this.name = n;
this.age = a;
System.out.println("含参构造器");
}
Person(String n){
this.name = n;
System.out.println("name构造器");
}
Person(int a){
this.age = a;
System.out.println("age构造器");
}
/**
* @return the name
*/
public String getName() {
return name;
};
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
};
/**
* @return the age
*/
public int getAge() {
return age;
};
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
};
};
2、关键字this
class person {
private long salary;
int age;
protected String interest;
public String name;
// this 可以修饰属性、方法、构造器,this为当前对象或创建的对象,
// this() 显示调用当前类重载构造器,
// 要求:this() 需要放置构造器中的首行;n个构造器最多使用 n-1 次 this()构造器
public person() {
// this 当前正在创建的对象
this.age = 0;
this.salary = 0;
this.interest = "food";
this.name = "baby";
System.err.println("i am person");
}
public person(int age){
// this 当前正在创建的对象
this.age = age;
}
public person(String name,int age){
// this() 显示调用当前类重载构造器: person(int age)
// this() 需要放置构造器中的首行
this(age);
this.name = name;
}
/**
* @param salary the salary to set
*/
public void setSalary(long salary) {
// this 当前对象
this.salary = salary;
}
/**
* @return the salary
*/
public long getSalary() {
return salary;
}
public void eat() {
System.out.println("eating...");
}
public void sleep() {
System.out.println("sleeping...");
}
}
3、权限修饰符
package01
package package01;
public class person {
// 权限修饰符大小: public (任何地方) > protected (子类) > 缺省 (同一个包)> private (类内部)
// class权限修饰符只有public 缺省
public String name;
int age;
protected int id;
private long salary;
public person() {
// TODO Auto-generated constructor stub
this.name = "aa";
this.age = 0;
this.id = 110;
this.salary = 0;
}
public person(String name){
this();
this.name = name;
}
public person(String name,int age){
this(name);
this.age = age;
}
public person(String name,int age,int salary){
this(name, age);
this.salary = salary;
}
public void eat() {
System.out.println("person eating ...");
}
public void eat(String name) {
System.out.println(name +" eating ...");
}
}
package01
package package01;
public class test02 {
public static void main(String[] args) {
person p = new person();
// 同一个package中可以调用public属性
System.out.println(p.name);
// 同一个package中可以调用缺省属性
System.out.println(p.age);
// 同一个package中可以调用protected属性
System.out.println(p.id);
// 同一个package中不可以调用private属性
// private属性只能在本类中调用
// System.out.println(p.salary);
p.eat("p");
}
}
package02
package package02;
import package01.person;
public class test01 {
public static void main(String[] args) {
person p1 = new person();
// 不同package中只能调用public属性
System.out.println(p1.name);
// 不同package中不可以调用缺省属性
// System.out.println(p1.age);
// 不同package中子类可以调用protected属性
// System.out.println(p1.id);
// 不同package中不可以调用private属性
// System.out.println(p1.salary);
p1.eat("p1");
}
}
class liming extends student {
// 不同package中子类可以调用protected属性
public liming() {
this.id = 422;
System.out.println("my id is "+this.id);
}
public void eat(String name, String food) {
System.out.println(name + " eat "+food);
}
}
4、super关键字
package package01;
public class student extends person{
protected int id;
public student() {
// super(args) 声明在构造器内部首行, 显示调用父类构造器;与this(args)冲突;
// 构造器内部super(args)和this(args)不能同时出现;
// 在构造器中,如果不显示调用this(args)或者super(args),则默认调用父类空参 super();
// 建议设计一个类时,提供一个空参构造器
// Object类,是所有类的父类;
super();
this.id = 11061;
super.id = 422326;
}
// 当子类与父类有同名属性id时,通过"super.id"显示调用父类声明的属性;通过"this.id"调用子类的属性;
public void show() {
System.out.println("my super id is "+ super.id);
System.out.println("my class id is "+ this.id);
}
}
5、多态性
package package01;
public class test3 {
public static void main(String[] args) {
// 多态性
// 子类对象的多态性,父类的引用指向子类对象
// 优先调用子类重写的方法;
// 要求:1、类的继承;2、子类对父类的方法的重写;
// 向上转型
person p1 = new student();
p1.eat();
// 程序运行时分为编译状态和运动状态
// 对于多态性,
// 编译时,"看左边",将此引用父类的属性、方法;
// 运行时,"看右边",将调用子类对象的实体
// p1.show();
// 使用强转(),向下转型
student s2 = (student)p1;
s2.show();
student s1 = new student();
s1.eat("s1");
test3 test = new test3();
test.eat(new student());
test.eat(new teacher());
}
public void eat(person p) {
p.eat();
// 多态父类调用子类方法
if (p instanceof student) {
((student) p).show();
}
if (p instanceof teacher) {
((teacher) p).teach();
}
}
}
6、单例设计模式
package package02;
import org.junit.Test;
public class test02 {
@Test
public void test() {
singleton s1 = singleton.getSingleton();
singleton s2 = singleton.getSingleton();
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
class singleton {
// 饿汉式单例模式
// 1、私有化构造器,只能在本类中调用
private singleton() {
System.out.println("singleton");
}
// 2、在类内部创建一个静态类变量(对象)
private static singleton singleton = new singleton();
// 3、提供公共方法,通过"类.类变量"的方法调用
public static singleton getSingleton() {
return singleton;
}
}
class singleton2 {
// 懒汉式单例模式,存在线程安全问题
private singleton2() {
System.out.println("singleton2");
}
private static singleton2 singleton= null ;
public static singleton2 getSingleton() {
if (singleton == null) {
singleton = new singleton2();
}
return singleton;
}
}