GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — JavaSE/Inner-class/README.MD at master · WhCannon/JavaSE

Expand file tree

/

README.MD

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
266 lines (235 loc) · 4.53 KB
/

README.MD

Copy path

File metadata and controls

266 lines (235 loc) · 4.53 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

内部类

  • 定义内部类
  • 内部类私有
  • 匿名内部类
  • 练习

内部类私有

我有一个人(人有身体,身体内有心脏。)
		class Body {
			private class Heart {
				public void operator() {
					System.out.println("心脏搭桥");
				}
			}
			public void method() {
				if(如果你是外科医生) {
					Heart h = new Heart();
					h.operator();
				}
			}
		}

匿名内部类

interface Inter {
	public abstract void show();
	public abstract void show2();
}

class Outer {
	public void method() {
		//一个方法的时候
		new Inter() {
			public void show() {
				System.out.println("show");
			}
		}.show();
		
		//如果我是很多个方法,就很麻烦了
		//那么,我们有没有改进的方案呢?
		Inter i = new Inter() { //多态
			public void show() {
				System.out.println("show");
			}
			
			public void show2() {
				System.out.println("show2");
			}
		};
		i.show();
		i.show2();
	}
}

class InnerClassDemo6 {
	public static void main(String[] args) {
		Outer o = new Outer();
		o.method();
	}
}
  • 匿名内部类的应用
interface Person {
	public abstract void study();
}
class PersonDemo {
	public void method(Person p) {
		p.study();
	}
}

class InnerClassTest2 {
	public static void main(String[] args) {
		PersonDemo pd = new PersonDemo();
		//匿名内部类在开发中的使用
		//匿名内部类的本质是继承类或者实现了接口的子类匿名对象
		pd.method(new Person(){
			public void study() {
				System.out.println("好好学习,天天向上");
			}
		});
	}
}

练习

1.补足代码 答案1

interface Inter
{
	void show(int a,int b);
	void func();
}
class Demo 
{
	public static void main(String[] args)
	{
		//补足代码,调用两个函数,用匿名内部类
		in.show(3,4);
		in.func();
	}
}

2.写出结果 答案2

class TD
{
	int y=6;
	class Inner
	{
		static int y=3;
		void show()
		{
			System.out.println(y);
		}
	}
}
class TC
{
	public static void main(String[] args)
	{
		TD.Inner ti=new TD().new Inner();
		ti.show();
	}
}

3.在空白处填写代码 答案3

class Demo
{
	public void func()
	{
		//位置1
	}
	class Inner
	{
	}
	public static void main(String[] args)
	{
		Demo d=new Demo();
		//位置2
	}
}

下列哪些写法是可行的:
A.位置1写new Inner();
B.位置2写new Inner();
C.位置2写new d.Inner();
D.位置2写new Demo.Inner();

4.补足代码 答案4

interface Test
{
	void func();
}
class Demo
{
	public static void main(String[] args)
	{
		//补足代码;(匿名内部类)调用show方法
		
	}
	void show(Test t)
	{
		t.func();
	}
}

[练习5]

要求输出30 20 10
class Outer {
	public int num = 10;
	class Inner {
		public int num = 20;
		public void show() {
			int num = 30;
			System.out.println(num);
			System.out.println(this.num);
			//System.out.println(new Outer().num);
			System.out.println(Outer.this.num);//限定this为Outer类
		}
	}
}

练习6

			按照要求,补齐代码
			interface Inter { void show(); }
			class Outer { //补齐代码 }
			class OuterDemo {
				public static void main(String[] args) {
					  Outer.method().show();
				  }
			}
			要求在控制台输出”HelloWorld”

答案1

Inter in=new Inter()
		{
			public void show(int a,int b)
			{}
			public void func()
			{}
		}

答案2

编译失败,因为非静态的内部类中不能定义静态成员

答案3

A.位置1写new Inner();//可以
B.位置2写new Inner();//不可以,主函数是静态的,所以内部类也必须是静态
C.位置2写new d.Inner();//不可以,相当于new new Demo().Inner();格式错误
D.位置2写new Demo.Inner();//不可以,Inner()必须是静态才能这样写

答案4

new Demo().show(new Test()
			{
				public void func(){}
			}
		);

答案6

从主函数分析
Outer.method().show();
1:Outer.method()可以看出method()应该是Outer中的一个静态方法。
2:Outer.method().show()可以看出method()方法的返回值是一个对象。
	又由于接口Inter中有一个show()方法,所以method()方法的返回值类型是一个接口。

class Outer { 
	//补齐代码
	public static Inter method() {
		//子类对象 -- 子类匿名对象
		return new Inter() {
			public void show() {
				System.out.println("HelloWorld");
			}
		};
	}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.