发布于2021-06-12 13:56 阅读(768) 评论(0) 点赞(16) 收藏(5)
内部类可以直接访问外部类的成员(public,private,protectd)
不能用static修饰
先实new一个外部类,才能new一个内部类
内部类可以使用 private 或 protected 来修饰,如果你不希望内部类被外部类访问可以使用 private 修饰符:
class OuterClass {
int x = 10;
class InnerClass {
int y = 5;
}
}
public class MyMainClass {
public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
}
}
//该例子来自菜鸟网站
静态内部类可以使用 static 关键字定义,静态内部类我们不需要创建外部类来访问,可以直接访问它:
静态内部类无法访问外部类的成员。
class OuterClass {
int x = 10;
static class InnerClass {
int y = 5;
}
}
public class MyMainClass {
public static void main(String[] args) {
OuterClass.InnerClass myInner = new OuterClass.InnerClass();
System.out.println(myInner.y);
}
}//该例子来自菜鸟网站
public class Test {
int a = 0;
int d = 0;
public void method() {
int b = 0;
final int c = 0;
final int d = 10;
class Inner {
int a2 = a; // 访问外部类中的成员
// int b2 = b; // 编译出错
int c2 = c; // 访问方法中的成员
int d2 = d; // 访问方法中的成员
int d3 = Test.this.d; //访问外部类中的成员
}
Inner i = new Inner();
System.out.println(i.d2); // 输出10
System.out.println(i.d3); // 输出0
}
public static void main(String[] args) {
Test t = new Test();
t.method();
}
}
/*
该例子来自C语言中文网
*/
package com.hyb.oop.D7;
import java.sql.SQLOutput;
class Person{
public void test(){
System.out.println("这是一个父类方法!");
}
}
public class Outner {
public static void main(String[] args) {
Outner outner = new Outner();
outner.method();
}
public int a=10;
private int b=100;
//外部类方法
public void method(){
//匿名类
//public int a=Outner.this.a;编译错误
final int a1=120;
Person person = new Person(){
//匿名类继承父类并重写其方法
final int a=Outner.this.a;
final int b=Outner.this.b;
@Override
public void test() {
System.out.println("重写父类方法!"+this.a+" "+a1+" "+this.b);
}
//如果匿名类位于一个方法中,则该类只能访问final修饰的成员
};
person.test();
}
/*
10
20
*/
}
//这里我遇到了一个问题,就是当匿名类的变量和匿名类所处的方法中的变量的时候,无论怎么调用,都是调用到匿名类里的变量,这是怎么回事?
package com.hyb.oop.D7;
//定义一个父类
public class Father {
public int strong1(){
return 9;
}
}
package com.hyb.oop.D7;
//母亲类
public class Mother {
public int strong2(){
return 10;
}
}
package com.hyb.oop.D7;
public class Son {
public static void main(String[] args) {
Son son = new Son();
System.out.println(son.method1());
System.out.println(son.method2());
/*
8
11
*/
}
public int method1(){
Father father = new Father() {
@Override
public int strong1() {
return super.strong1() - 1;
}
};
return father.strong1();
}
public int method2(){
Mother mother = new Mother() {
@Override
public int strong2() {
return super.strong2() + 1;
}
};
return mother.strong2();
}
}
原文链接:https://blog.csdn.net/weixin_47303191/article/details/117715759
作者:java之恋
链接:http://www.javaheidong.com/blog/article/222385/669817a309cc80ced576/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!