1.代码块:
1.类的属性的默认初始化和显示初始化;
2.执行代码块的代码;
3.执行构造器的代码
public class Person {
String name;
public Person() {
this.name = "张三";
System.out.println("执行的是构造方法");
}
//非静态代码块
{
System.out.println("执行的是非静态代码块");
}
}
public class Test {
public static void main(String[] args) {
new Person();
}
}
程序运行中,静态代码块先执行且只只进行一次。
2.final关键字:
1.修饰的类不能被继承;
2.标记的方法不能被重写;
3.标记的变量变常量,记全大写。
3.抽象(abstract)类:
用来作为父类被继承,只能修饰类
public abstract class Employee {
public Employee() {
}
int id;
String name;
double salary;
public abstract void work();
}
class CommonEmployee extends Employee{
public void setCommonEmployeeInfo(int id,String name,double salary) {
super.id = id;
super.name = name;
super.salary = salary;
}
public void getCommonEmployeeInfo() {
System.out.println(super.id);
System.out.println(super.name);
System.out.println(super.salary);
}
@Override
public void work() {
System.out.println("这是一个普通员工");
}
}
class Manager extends Employee{
double bonus;
public void setManagerInfo(int id,String name,double salary,double bonus) {
super.id = id;
super.name = name;
super.salary = salary;
//注意这里是自己的属性,不是继承来的
this.bonus = bonus;
}
public void getManagerInfo(int id,String name,double salary,double bonus) {
System.out.println(super.id);
System.out.println(super.name);
System.out.println(super.salary);
System.out.println(this.bonus);
}
@Override
public void work() {
System.out.println("这是一个领导");
}
}
public class Test {
public static void main(String[] args) {
CommonEmployee ce = new CommonEmployee();
ce.work();
ce.setCommonEmployeeInfo(123, "张三", 6353.3);
ce.getCommonEmployeeInfo();
}
}
4.模板方法设计模式:
5.接口(interface):
实现多重继承
public abstract class Person1 {
int age;
String name;
int sex;
public abstract void showInfo();
}
/**
* 描述会唱歌的厨子是老师
* @author Daniel
*
*/
public class SCTeacher extends Person1 implements Cooking,Sing{
String course;//教的科目
public void setInfo() {
super.age = 27;
super.name = "王富贵";
super.sex = 1;
this.course = "语文";
}
@Override
public void showInfo() {
System.out.println("会唱歌的厨子的老师的信息是:");
System.out.println(super.age);
System.out.println(super.name);
System.out.println(super.sex);
System.out.println(this.course);
}
@Override
public void singing() {
System.out.println(super.name + "老师拿手的歌曲是飘向北方");
}
@Override
public void fry() {
System.out.println(super.name + "老师拿手的厨艺是炒菜");
}
}
/**
* 厨艺的接口
* @author Daniel
*
*/
public interface Cooking {
void fry();//炒菜
}
/**
* 歌唱的接口
* @author Daniel
*
*/
public interface Sing {
void singing();//唱法
}
public class Test {
public static void main(String[] args) {
SCTeacher sct = new SCTeacher();
sct.setInfo();
sct.showInfo();
sct.fry();
sct.singing();
Cooking c = new SCTeacher();
c.fry();
}
}
6.工厂方法:
7.内部类:
/**
* 内部类主要解决不能多重继承问题
* @author Daniel
*
*/
public class Test3 {
int i;
public int z;
private int k;
class A{
public void setTest3Fields() {
Test3.this.i = 1;
Test3.this.z = 2;
Test3.this.k = 3;
}
}
//如果内部类是static,则不能用外部类的成员变量
static class c{
}
public void setInfo() {
new A().setTest3Fields();
}
public void showInfo(){
System.out.println(this.i);
System.out.println(this.z);
System.out.println(this.k);
}
public static void main(String[] args) {
Test3 t = new Test3();
t.setInfo();
t.showInfo();
}
}