block 块
1、格式:{} 自成作用域
2、在方法中的块称为普通块或局部块,与方法一起执行
3、类中方法外的称为构造块,与构造器一起使用,先于构造器执行,有多个时按顺序
4、static静态块,类加载时就被加载到内存中,先于构造块执行,多个时按顺序
public void offWord() {
{
//block:方法中的为方法块也叫普通语句块,跟随方法运行
System.out.println("offWord方法中的方法块。。。");
}
System.out.println("会计工作忙,要晚上8点下班");
}
//block:类中方法外的块,构造块与构造器一起使用,先与构造器执行,有多个时按顺序执行
{
System.out.println("构造块1。。。");
}
{
System.out.println("构造块2。。。");
}
//block:静态块,类加载时就被加载到内存中,先于构造块执行,多个时按顺序
static {
System.out.println("静态块1。。。");
}
static {
System.out.println("静态块2。。。");
}
包package和导入import
1、包:为了更好的管理项目
2、同一个包中的类名不能重复
3、不同包下可以存在同名类
4、访问的类不在同一包中的时候,需要使用import导入需要的包
5、导入类中的静态成员变量
package test;
//导入类中的静态成员变量
import static java.lang.Math.PI;
//访问的类不在同一包中的时候,需要使用import导入需要的包
```java
import extends03.ExtendsDemo01;
public class Area {
public static void main(String[] args) {
int r=4;
double area=PI*r*r;
System.out.println("圆的面积是:"+area);
ExtendsDemo01 e=new ExtendsDemo01();
}
}
面向对象之继承
1、关键字extends
2、子类继承父类,可以使用父类的成员,子类也可以自定义自己的内容
3、java中为单继承,一个子类只能有一个父类,一个父类可以有多个子类
重写override
1、方法的重写是在不同类中
2、类的继承或接口的实现会有重写
3、重写的方法的签名一致
4、父类方法为static的,子类重写的也必须是static的
5、子类中重写方法的权限>=父类中该方法的访问权限
6、子类中重写方法的返回类型<=父类中该方法的返回类型
super
1、子类构造器首行调用父类的构造方法
2、区分子类与父类的同名变量
3、指代父类对象
this和super的比较
相同点:
1、都可以在构造器首行调用其他构造器
2、都可以区分同名变量
3、都可以指代对象,所有不能在static中使用
不同点:
1、this在构造器首行调用的是本类的其他构造器;super在构造器首行调用的是父类的其他构造器
2、this区分同名变量是成员与布局的变量;super区分的是父类与子类的同名变量
3、this指代当前对象;super指代的是父类对象
final:
1、被final修饰的类不能被继承
2、被final修饰的方法不能被重写
3、被final修饰的变量为常量;若数据类型为引用类型,不能再次new,但可以改变对象的属性值
package test;
//员工类
public class Employee {
public String name;
public int age;
public int id;//工号
public String branch;//部门
//被final修饰的变量不能被其他类访问
final String zw;
//被final修饰的方法不能被其他类访问,不能被重写
final void print() {
System.out.println("职位:"+zw);
}
public void onWord() {
System.out.println("9点打卡上班");
}
public void offWord() {
System.out.println("5点打卡下班");
}
public Employee() {
this.zw = "";
System.out.println("父类空构造器");
}
public Employee(String name,String branch) {
this.zw = "";
this.name=name;
this.branch=branch;
}
}
//会计类继承Employee,java中为单继承,一个子类只能有一个父类,一个父类可以有多个子类;子类继承父类后可以使用父类的成员内容,
//也可以定义自己的成员
class Accounter extends Employee{
//子类自定义的成员变量
public int salary;
public Accounter() {
//使用super关键字在子类的构造器首行调用父类构造器
//如果没有显示调用,会默认调用父类空构造
super();
System.out.println("空构造");
}
//构造器的重载
public Accounter(String name,int age,int id,String branch,int salary) {
//使用super调用父类的构造器
super(name,branch);
this.age=age;
this.id=id;
this.salary=salary;
}
//子类自定义的成员方法
public void salary() {
System.out.println("会计人员的工资是:"+salary);
}
//对父类方法的重写override
public void offWord() {
{
//block:方法中的为方法块也叫普通语句块,跟随方法运行
System.out.println("offWord方法中的方法块。。。");
}
System.out.println("会计工作忙,要晚上8点下班");
}
//block:类中方法外的块,构造块与构造器一起使用,先与构造器执行,有多个时按顺序执行
{
System.out.println("构造块1。。。");
}
{
System.out.println("构造块2。。。");
}
//block:静态块,类加载时就被加载到内存中,先于构造块执行,多个时按顺序
static {
System.out.println("静态块1。。。");
}
static {
System.out.println("静态块2。。。");
}
}
package test;
public class Test01 {
public static void main(String[] args) {
Employee e=new Employee("张三","技术部");
e.onWord();
e.offWord();
System.out.println("===========================");
Accounter a=new Accounter("李四",32,0327001,"会计部",5000);
a.onWord();
a.offWord();
a.salary();
}
}
结果:
9点打卡上班
5点打卡下班
===========================
静态块1。。。
静态块2。。。
构造块1。。。
构造块2。。。
9点打卡上班
offWord方法中的方法块。。。
会计工作忙,要晚上8点下班
会计人员的工资是:5000
object:
java中所有类的基类,没有显示导入的时候会自动默认为程序导入Object包