黑马程序员 java 基础 毕向东 面向对象 内部类访问规则

时间:2023-02-11 12:31:09

                                                                 ------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

黑马程序员 java 基础 毕向东 第9天 面向对象 内部类访问规则


内部类

内部类的访问规则:

1:内部类可以直接访问外部类的成员,包括私有 的 2:外部类访问内部类,必须建立内部类对象 直接访问内部类的成员: 之所以可以直接访问外部类的成员,因为内部类持有内部类的引用,写法为 内部类.this 而 外部类访问内部类,必须先建立内部类的对象
访问格式: 1:当内部类定义在外部类的成员位置上,且非私有,可以在外部其他类中, 可以直接建立内部类对象 格式: 外部类名.内部类名   变量名=外部类对象.内部类对象; 2:当内部类在成员位置上,就可以被成员修饰符修饰,比如private
static :内部类就具备了静态特性;
当描述事物时,事物内部还有事物,该事物用内部类表示 因为内部类事物在使用外部类 的内容

匿名内部类:

1:就是内部类的简写格式 2:定义匿名内部类的前提       内部类必须继承一个类或者实现接口 3:匿名内部类的格式:             new 父类或者接口(){                     定义子类的内容             }
4:匿名内部类就是一个匿名子类对象,这个对象有点胖,也可以理解为带内容的对象 5:匿名内部类中定义的方法最好不要超过3个
相关代码 ------------------------------------------------------------------------------------
package day09;

public class InnerClassDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Outer outer=new Outer();
outer.function(1);
outer.function(2);
}
}
class Outer{
private int x=3;
private static int xx=22;
void function(final int a){
final int y=4;
class Inner2{
void show(){
System.out.println("innser show");

System.out.println(a);
System.out.println(y);
//内部类定义在局部,不可以被成员修饰符修饰
//可以直接访问外部类中的成员,以为持有外部类的引用
//但不可以访问所在局部中的变量。除非被final修饰

}
}
new Inner2().show();
}



}
--------------------------------------------------------------------------------
package day09;

public class InnerClassDemo3 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Outer3 outer3=new Outer3();
outer3.method();

//如何访问静态内部类呢
new Outer3.StaticInner().function();
}

}
class Outer3{
private int x=3;
private static int xx=22;
//class Inner{
//当内部类中定义了静态成员,该内部类必须为静态的
//当外部类的静态方法访问内部类时。内部类必须为静态的
//int x=6;
//void function(){
//int x=4;
//System.out.println("innser :"+x);
//System.out.println("innser :"+this.x);
//System.out.println("outer:"+Outer.this.x);
//}
//}

public static void method2(){
StaticInner.function();
//new Inner2().show() 不成功
}
static class StaticInner{//静态内部类
//int x=6;
static void function(){
//int x=4;
System.out.println("innser 3:"+xx);
//System.out.println("innser :"+this.x);
}
}
void method(){
//Inner inner=new Inner();
//inner.function();
StaticInner inner=new StaticInner();
inner.function();
class Inner2{
void show(){
System.out.println("innser show");
}
}
}


//
//class Body{
//private class XinZang{
//
//}
//public void show() {
//new XinZang();
//}
//}
}

--------------------------------------------------------------------------------
package day09;

public class InnerClassDemo4 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Outer4().function();
}
}
abstract class Absdemo{
abstractvoid show1();
abstractvoid show2();
abstractvoid show3();
}
class Outer4{
int x=3;
/*
class Inner extends Absdemo{
void method(){
System.out.println("method :"+x);
}

@Override
void show() {
// TODO Auto-generated method stub
System.out.println("show :"+x);
}
}*/

public void function() {
//new Inner().method();
//new Inner().show();
Absdemo aaAbsdemo=new Absdemo(){
int numm=99;

@Override
void show1() {
// TODO Auto-generated method stub
System.out.println("show neibu :"+x);
System.out.println("show neibu :"+numm);
}

@Override
void show2() {
// TODO Auto-generated method stub
System.out.println("show neibu :"+x);
}

@Override
void show3() {
// TODO Auto-generated method stub
System.out.println("show neibu :"+x);
}
void show34() {
// TODO Auto-generated method stub
System.out.println("show neibu :"+x);
}
};
aaAbsdemo.show1();
aaAbsdemo.show2();
aaAbsdemo.show3();
//aaAbsdemo.show34();//编译失败
}
}


--------------------------------------------------------------------------------
package day09;

public class InnerClassTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Test.funtion().method();
Inter inter=Test.funtion();
inter.method();
show(new Inter() {
@Override
public void method() {
// TODO Auto-generated method stub
System.out.println("0000000000000000");
}
});
new Object(){
void funtion(){
System.out.println("object");
}
}.funtion();
//Test.funtion() :test 类中有静态方法funtion
//.method(): funtion 方法结果为inter 对象
}
public static void show(Inter args) {
// TODO Auto-generated method stub
args.method();
}
}
interface Inter{
void method();
}
class Test{
//补足代码,通过匿名类
static Inter funtion(){
return new Inter(){
@Override
public void method() {
// TODO Auto-generated method stub
System.out.println("Test -》funtion-》Inter-》method");
}
};
}
}

测试题:
package day09;

class TestB extends TestA{
TestB(){
System.out.println("Test B");
}
}
public class TestA {
TestA(){
System.out.println("Test A");
}

public static void main(String[] args) {
TestB testB=new TestB();
}

}
/*
class Super
{
int i=0;
public Super(String s)
{
i=1;
}
}
class DemoA extends Super
{
public DemoA(String s)
{

i=2;
}
public static void main(String[] args)
{
DemoA d=new DemoA("yes");
System.out.println(d.i);
}
}
*/
/*
class Super
{
public int get(){return 4;}

}
class Demo15 extends Super
{
public long get(){return 5;}
public static void main(String[] args)
{
Super s=new Demo15();
System.out.println(s.get());
}
}
*/
class DemoAA
{
public void func()
{
//位置1;
new Inner();

}
class Inner{}
public static void main(String[] args)
{
DemoAA d=new DemoAA();
// 位置2
//new Inner();//不可以,因为主函数是静态的。如果访问inner需要被static修饰。
new DemoAA().new Inner();
}
}
/*
*
* A.在位置1写 new Inner();//ok
B.在位置2写 new Inner();
C.在位置2写 new d.Inner();//错误,格式错误。new new Demo().Inner();
D.在位置2写 new Demo.Inner();//错误,因为inner不是静态的。

*/


《黑马程序员 java 基础 毕向东 面向对象 内部类访问规则》 介绍了java中内部类的使用方法和使用情景。