1. 关键字static:
有一些属性和方法与对象无关,而与类有关,这些属性和方法就可以定义为static,存储在永久区内。
可以理解为这个类中所有的对象都包含的属性和方法,如中国人的类中都包含“国家=中国”这个属性。
被static的属性称为类属性,方法称为类方法。
特点:随着类的加载而加载,优先于对象存在,被所有对象所共享。
package com.hike.javase.statictest;
public class Employee {
//静态属性,和对象无关
public static String company = "hike";
//静态方法,和对象无关,静态成员之间可以直接进行互访
public static void test() {
System.out.println("公司:" + company);
}
private static int num = 1;
private int id;
private String name;
private int age;
private double salary;
public Employee() {
super();
}
public Employee(String name, int age, double salary) {
super();
this.id = num++;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}
package com.hike.javase.statictest;
public class EmplolyeeTest {
public static void main(String[] args) {
// 静态成员不需要对象,所以使用简单
Employee.test();
Employee.company = "hikeee";
Employee.test();
Employee e1 = new Employee("张三",20,2000);
System.out.println(e1.toString());
Employee e2 = new Employee("张三",20,20000);
System.out.println(e2.toString());
Employee e3 = new Employee("张三",20,200000);
System.out.println(e3.toString());
Employee e4 = new Employee("张三",20,2000000);
System.out.println(e4.toString());
}
}
2. 单例设计模式
只允许有一个对象。
3. 静态语句块
static{} 给类进行初始化,在类创建时执行,且只执行一次,类似于构造器(给对象进行初始化)
{}:非静态语句块,在创建对象时执行,每次new一个对象执行一次,先于构造器执行。
4. final
final标记的类不能被继承。
final标记的方法不能被子类重写。
final标记的变量称为常量,名称大写,且只能被赋值一次。
5. 抽象类
具体类:某种事物的抽象定义。
抽象类:某类不同事物的统一抽象定义。
抽象类可以包含抽象方法,抽象类不可以创建对象。
抽象方法:只有方法签名,没有方法体,不能执行,只能表明具有哪种方法。想要使用抽象方法必须重写,也就是实现方法的过程。
package com.hike.javase.abstracttest;
/**
* @author 华硕
*
*/
public abstract class Pet {
private String name;
private int age;
private String weight;
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 String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public Pet(String name, int age, String weight) {
super();
this.name = name;
this.age = age;
this.weight = weight;
}
public Pet() {
super();
}
@Override
public String toString() {
return "pet [name=" + name + ", age=" + age + ", weight=" + weight + "]";
}
public abstract void speak();
public abstract void eat();
}
package com.hike.javase.abstracttest;
public class Cat extends Pet{
private String color;
public Cat() {
super();
}
public Cat(String name, int age, String weight, String color) {
super(name, age, weight);
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return super.toString() + ",color: " + color;
}
@Override
public void speak() {
System.out.println("小猫在叫:喵喵喵");
}
@Override
public void eat() {
System.out.println("小猫在吃鱼");
}
}
package com.hike.javase.abstracttest;
public class PetTest {
public static void main(String[] args) {
Pet p = new Cat("小白",1,"20","白色");
p.eat();
p.speak();
System.out.println(p.toString());
}
}