package com.test01;
public class A {
// 内部类 B 继承 TestPojo 和实现 TestInterface 接口
class B extends TestPojo implements TestInterface{
//内部类 B 自己的方法
public void run(){
System.out.println("我正在跑!");
}
//重写的接口方法
public void testf() {
System.out.println("实现接口!");
}
}
// 方法里面调用内部类
public void test(){
B b = new B() ;
b.testf() ; // 掉用重写接口方法
b.run() ; // 调用自己的方法
b.testpojo() ; // 调用继承父类的方法
}
// main 方法测试
public static void main(String[] args) {
A a = new A() ;
a.test() ;
}
}
// 定义了一个接口 ,方法为testf()
interface TestInterface{
public void testf() ;
}
// 定义了一个普通类 方法testpojo()
class TestPojo{
public void testpojo(){
System.out.println("我是简单的pojo类");
}
}
// 实现 调用内部类里面的方法
class Textone{
public static void main(String[] args) {
A.B b = new A().new B() ; //调用类A里面的内部类B
/** 等同于 下面代码
* A a = new A() ;
* A.B b = a.new B() ;
* */
b.testf() ; // 掉用重写接口方法
b.run() ; // 调用自己的方法
b.testpojo() ; // 调用继承父类的方法
}
}
package com.test01;
public class PerTest {
public void test(){ // 定义一个方法
class Ne{ // 定义一个方法内部类
public void fle(){ // 定义方法内部类的方法
System.out.println("我正在飞翔!");
}
} ;
new Ne().fle() ; //调用内部类的方法
}
public static void main(String[] args) {
new PerTest().test() ; //测试
}
}
package com.anonymous;
public class AbstractClass {
public void test(){ //方法为test
TestA a = new TestA(){ //实现抽象类
@Override
public void run() { //实现抽象类的方法
System.out.println("我正在使用抽象匿名内部类");
}
} ;
a.run() ; //调用内部类的方法
}
public static void main(String[] args) {
new AbstractClass().test() ; //测试
}
}
//定义一个抽象类 TestA 抽象方法为run()
abstract class TestA{
public abstract void run() ;
}
package com.anonymous;
public class TestAnonymous {
MyInterface m = new MyInterface(){ //实现接口
public void eat() { //重写MyInterface 接口方法
System.out.println("我正在吃饭!");
}
} ;
public void ss(){ //方法ss
m.eat() ; //调用重写的方法
}
public static void main(String[] args) {
new TestAnonymous().ss() ; //测试
}
}
//定义一个接口 方法为 eat
interface MyInterface{
public void eat() ;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有