package com.itehima.Demo11;
/**
* @program: spring
* @description: ${description}
* @author: Mr.zw
* @create: 2021-05-12 20:20
**/
//1.内部类可以直接使用外部类的成员
//外部类使用内部类的成员,要先创建内部类的对象
public class Person {//外部类
//成员变量
int num = 10;
//成员方法
public void fun(){
Hear h = new Hear();
System.out.println(h.a);
}
//成员内部类
class Hear{
int a = 100;
public void method(){
System.out.println(num);
}
}
}
package com.itehima.Demo11;
/**
* @program: spring
* @description: ${description}
* @author: Mr.zw
* @create: 2021-05-12 20:28
**/
public class PersonTest {
public static void main(String[] args) {
// 创建内部类对象, 使用成员
// 外部类名.内部类名 对象名 = new 外部类名().new 内部类名();
Person.Hear h = new Person().new Hear();
System.out.println(h.a);
}
}
重写接口中的方法
创建子类对象
package com.itehima.Demo11.Demo02;
/**
* @program: spring
* @description: ${description}
* @author: Mr.zw
* @create: 2021-05-12 20:50
**/
public interface FlyAble {
void fly();
}
package com.itehima.Demo11.Demo02;
/**
* @program: spring
* @description: ${description}
* @author: Mr.zw
* @create: 2021-05-12 20:51
**/
public class Bird implements FlyAble {
@Override
public void fly() {
System.out.println("鸟会飞");
}
}
package com.itehima.Demo11.Demo02;
/**
* @program: spring
* @description: ${description}
* @author: Mr.zw
* @create: 2021-05-12 20:52
**/
/*
匿名内部类:
前提: 存在类或者接口
格式:
new 类名() {}
new 接口名() {}
本质:
new 类名() {} -> 代表继承了该类的子类对象
new 接口名() {} -> 代表实现了该接口的实现类对象(子类对象)
*/
public class MyTest {
public static void main(String[] args) {
// 这段代码的目的:
// 目的: 调用fun方法
// 付出代价: 创建了一个类
// Bird b = new Bird();
// 调用fun方法
// fun(b);
// new Bird() -> FlyAble接口的实现类对象(子类对象)
// new FlyAble() {} -> FlyAble接口的实现类对象(子类对象)
fun(new FlyAble() {
@Override
public void fly() {
System.out.println("又上天了!~");
}
});
}
// 有一个方法, 方法的参数列表是 接口
public static void fun(FlyAble f) {
f.fly();
}
}
一段有意思的代码
package com.itehima.Demo11.Demo02;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
* @program: spring
* @description: ${description}
* @author: Mr.zw
* @create: 2021-05-12 21:02
**/
public class Demo01 {
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("起床了");
}
}, new Date(121,4,12,21,7,50));
}
}
原文:https://www.cnblogs.com/demowei/p/14761722.html