面向对象14:static关键字详解
static修饰变量时
用static修饰的变量是静态变量,其他为非静态变量。
静态变量可以直接通过类名访问,非静态变量不行。
package com.oop.demo11;
public class Student {
private static int age;
private double score;
public static void main(String[] args) {
Student student = new Student();
//不能通过类名访问非静态变量
System.out.println(Student.age);//通过类名直接访问静态变量
System.out.println(student.score);//通过对象访问静态变量
System.out.println(student.age); //通过对象访问非静态变量
}
}
static修饰方法时
用static修饰的方法是静态方法,其他为非静态方法。
注意点:
- 不能直接调用非静态方法,需要通过 new 对象.方法 来调用;
- 静态方法可以直接调用;
- 非静态方法可以直接访问这个类中的静态方法;
- 静态方法不能调用其他方法;
- 一个类的修饰符如果有final,就不可以被继承了,相当于断子绝孙了。
package com.oop.demo11;
public class Student {
public void run(){
//非静态方法可以调用静态方法
go();
}
public static void go(){
}
public static void main(String[] args) {
Student student = new Student();
//调用非静态方法
student.run();
new Student().run();
//调用静态方法
go();
Student.go();
}
}
静态代码块
package com.oop.demo11;
public class Person {
//第二步执行,可赋初始值
{
//匿名代码块
System.out.println("匿名代码块");
}
//第一步执行,且只执行一次
static {
//静态代码块
System.out.println("静态代码块");
}
//第三步执行
public Person() {
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person = new Person();
System.out.println("=====================");
Person person1 = new Person();
}
}
静态导入包
package com.oop.demo11;
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test {
public static void main(String[] args) {
System.out.println(Math.random());
//静态导入
System.out.println(random());
System.out.println(PI);
}
}
面向对象15:抽象类
package com.oop.demo12;
//abstract 抽象类
//抽象类本质是一个类,逃不了extends关键字,且只能单继承
//接口可以多继承
public abstract class Action {
//约束,有人帮我们实现
//abstract抽象方法,只有方法的名字,没有方法的实现
public abstract void doSomething();
}
//子类
package com.oop.demo12;
//抽象类中的所有方法,继承了它的子类,都必须要实现它的方法,除非子类也是抽象类
public class A extends Action{
@Override
public void doSomething() {
}
}
总结
- 抽象类不能new,只能靠子类去实现它,约束!
- 抽象类中可以写普通方法
- 抽象方法必须在抽象类中
- 抽象的抽象:约束!
思考题
- 抽象类不能new对象,那么它存在构造器吗?
- 抽象类存在的意义是什么?
面向对象16:接口的定义与实现
package com.oop.demo13;
//接口定义的关键字为interface,接口都需要有实现类
public interface UserService {
//接口定义的为静态常量
public static final int AGE = 99;
//接口中,所有方法的定义都是抽象的 public abstract,因此可将其省略
public abstract void add(String name); //增
void delete(String name); //删
void update(String name); //改
void query(String name); //查
}
package com.oop.demo13;
public interface TimeService {
void timer();
}
//接口
package com.oop.demo13;
//实现了接口的类,就需要重写接口的方法
//利用接口可以实现多继承
public class UserServiceImpl implements UserService{
@Override
public void add(String name) {
}
@Override
public void delete(String name) {
}
@Override
public void update(String name) {
}
@Override
public void query(String name) {
}
@Override
public void timer() {
}
}
接口总结
- 约束
- 定义一些方法,让不同的人实现
- 方法都为:public public abstract
- 常量都为:public static final
- 接口不能被实例化,接口没有构造方法
- implements关键字可以实现多个接口
- 实现接口必须要重写接口中的方法
面向对象17:N种内部类
成员内部类
package com.oop.demo14;
public class Outer {
private int id = 10;
public void out(){
System.out.println("这是外部类方法");
}
public class Inner{
public void in(){
System.out.println("这是内部类方法");
}
//内部类可以获得的私有属性
public void getId(){
System.out.println(id);
}
}
}
//测试
package com.oop;
import com.oop.demo14.Outer;
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
outer.out();
//通过外部类调用内部类
Outer.Inner inner = outer.new Inner();
inner.getId();
inner.in();
}
}
静态内部类
静态内部类无法直接访问非静态的属性。
package com.oop.demo14;
public class Outer {
private int id = 10;
public void out(){
System.out.println("这是外部类方法");
}
public static class Inner{
public void in(){
System.out.println("这是内部类方法");
}
}
}
//具体的学到lambda表达式后再详细推导
//测试
package com.oop;
import com.oop.demo14.Outer;
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
outer.out();
}
}
在一个Java文件写两个不同的类
package com.oop.demo14;
public class Outer {
private int id = 10;
public void out(){
System.out.println("这是外部类方法");
}
}
//一个Java类中可以有多个class类,但只能有一个public class
//这个类中可以写一些测试方法
class A{
public static void main(String[] args) {
}
}
局部内部类
在方法体内写的类叫局部内部类
package com.oop.demo14;
public class Outer {
//局部内部类
public void method(){
class Inner{
public void in(){
}
}
}
}
匿名内部类
package com.oop.demo14;
public class Test {
public static void main(String[] args) {
//正常调用对象会给对象一个名字
Apple apple = new Apple();
//没有名字初始化类,不用将实例保存到变量中
new Apple().eat();
//这个方法会返回userservice这个对象
UserService userservice = new UserService(){
@Override
public void hello() {
}
};
}
}
class Apple{
public void eat(){
System.out.println("eat Apple");
}
}
interface UserService{
void hello();
}