黑马程序员——Java基础——继承之内部类(四)

时间:2023-02-16 16:51:12
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ------- 内部类:
package cn.fuxi.neibulei;
/**
* 定义:将一个类定义在另一个类的里面.里面那个类就称为内部类(内置类,嵌套类).
* 特点:1.内部类可以直接访问外部类中的成员,包括私有成员.
* 2.而外部类要访问内部类中的成员必须要建立内部类对象.
* 内部类的位置:
* 内部类定义在成员位置上,可以被private|static成员修饰符修饰.
* 被static修饰的内部类只能访问外部类中的静态成员.
* 如果内部类是静态的,内部类成员也是静态的,可以不用创建对象,直接调用
* 如果内部类中定义了静态成员,该内部类也必须是静态的!
* 为什么内部类能直接访问外部类中的成员呢?
* 那是因为内部类持有了外部类的引用,外部类名.this.
*/
class Outer{
/*
* 内部类的设计:
* 分析事物时,发现该事物描述中还有事物,而且这个事物还在访问被描述事物的内容,这时候就定义内部类.
*
*/
private static int num = 3;
static class Inner{//内部类
static void show(){
System.out.println("Inner show run "+num);
}
}
public void method(){
Inner in = new Inner();
in.show();
}
}

public class NeiBuLeiDemo {
public static void main(String[] args) {
/* Outer ou = new Outer();
ou.method();
//直接访问内部类中的成员
Outer.Inner in = (new Outer()).new Inner();
in.show();
*/
//如果内部类是静态的,相当于一个外部类
Outer.Inner in = new Outer.Inner();
in.show();
Outer.Inner.show();//全静态直接调用,不要创建对象
}
}
运行结果:
Inner show run  3
Inner show run  3
package cn.fuxi.neibulei;
class Outer2{
int num = 3;
class Inner2{
int num =4;
void show(){
int num = 5;
System.out.println(num);
System.out.println(this.num);
System.out.println(Outer2.this.num);
}
}
void method(){
new Inner2().show();
}
}

public class NeiBuLeiDemo2 {

public static void main(String[] args) {
Outer2 o2 = new Outer2();
o2.method();
//new Outer2().method();
}

}

运行结果:
5 4 3
package cn.fuxi.neibulei;
/*
*内部类定义在局部位置上,也可以直接访问外部类中的成员.
*同时也可以访问所在局部中被final修饰的局部变量.
*/
class Outer3{
int num = 3;
void method(final int y){
final int x = 9;
class Inner{
void show(){
System.out.println("show..." + x + "," + y);
}
}
Inner in = new Inner();
in.show();
}
}

public class NeiBuLeiDemo3 {
public static void main(String[] args) {
new Outer3().method(4);

}

}
运行结果:
show...9,4
package cn.fuxi.neibulei;
/**
* 匿名内部类:
* 定义:就是内部类的简化写法.
* 前提:内部类可以继承或实现一个外部类或者接口.
* 格式:new 外部类或者接口名(){覆盖类火灾接口中的代码,也可自定义内容}
* 简单理解:就是建立一个带你内容的外部类或者接口的子类匿名对象.
* 什么时候使用匿名内部类呢?
* 通常使用方法是接口类型参数,并且该接口中的方法不超过三个,可以将匿名内部类作为参数传递.
* 好处:增强阅读性.
*/
abstract class Demo{
abstract void show();
}
class Outer4{
int num = 4;
void method(){
new Demo(){//匿名内部类
void show(){
System.out.println("show...."+num);
}
}.show();
}
}
public class NeiBuLeiDemo4 {

public static void main(String[] args) {
new Outer4().method();

}

}
运行结果:
show....4
package cn.fuxi.neibulei;
interface Inter{
void show1();
void show2();
}
class Outer5{
public void method(){
Inter in = new Inter(){
public void show1(){
System.out.println("Inter show1 run...");
}
public void show2(){
System.out.println("Inter show2 run...");
}
};
in.show1();
in.show2();
}
}
public class NeiBuLeiDemo5 {
public static void main(String[] args) {
Outer5 o5 = new Outer5();
o5.method();
//new Outer5().method();
}

}
运行结果:
Inter show1 run...
Inter show2 run...

匿名内部类:
package cn.fuxi.neibulei;
interface Inter2{
void show1();
void show2();
}
/*
*通常的使用场景之一:
*当函数参数是接口类型时,而且接口中的方法不超过三个.
*可以用匿名内部类作为实际参数经行传递.
*/

public class NeiBeiLeiDemo6 {
public static void main(String[] args) {
show(new Inter(){
public void show1(){
System.out.println("show1 run");
}
public void show2(){
System.out.println("show2 run");
}
});
}
public static void show(Inter in){
in.show1();
in.show2();
}
}
运行结果:
show1 run
show2 run

对象的初始化:
package cn.fuxi.neibulei;
class Fu7{
int num = 9;
{
System.out.println("Fu");
}
Fu7(){
super();//Object
//显示初始化
//构造代码块初始化
show();
}
void show(){
System.out.println("fu show "+ num);//被子类覆盖
}
}
class Zi7 extends Fu7{
int num = 8;
{
System.out.println("Zi");
}
Zi7(){
super();
//显示初始化
//构造代码块初始化
show();
}
void show(){
System.out.println("zi show " + num);
}
}

public class ChuShiHuaDemo {
public static void main(String[] args) {
new Zi7();

}
}
运行结果:
Fu
zi show 0
Zi
zi show 8