The most important role of Java internal classes – implementing multiple inheritance

< span style="color:rgb(51,51);font-family:Helvetica,sans-serif;font-size:14px;"> 1、 Inner class definition: the class placed inside a class is called inner class.

public interface Incrementable{
void increment();
}

concrete class

public class Example {
private class InsideClass implements InterfaceTest {
public void test() {
System.out.println("这是一个测试");
}
}
public InterfaceTest getIn() {
return new InsideClass();
}
}

Client program

public class TestExample {
public static void main(String args[]){
Example a=new Example();
InterfaceTest a1=a.getIn();
a1.test();
}
}

From this code, I only know the name of example

public class TagBean {
private String name="liutao";
private class InTest{
public InTest(){
System.out.println(name);
}
}
public void test() {
new InTest();
}
public static void main(String args[]){
TagBean bb=new TagBean();
bb.test();
}
}

The variable name is a private variable defined in tagbean. This variable has unconditional access to system. In the inner class out. println(name);

public class Example1 {
public String name() {
return "liutao";
}
}

Class II

public class Example2 {
public int age() {
return 25;
}
}

Class III

public class MainExample{
private class test1 extends Example1{
public String name() {
return super.name();
}
}
private class test2 extends Example2 {
public int age(){
return super.age();
}
}
public String name(){
return new test1().name();
}
public int age() {
return new test2().age();
}
public static void main(String args[]) {
MainExample mi=new MainExample();
System.out.println("姓名:"+mi.name());
System.out.println("年龄:"+mi.age());
}
}

Please pay attention to class 3, which implements two internal classes test1 and test2 respectively. Test1 class inherits example1 and test2 inherits example2. In this way, our class 3 mainexample has the methods and properties of example1 and example2, and indirectly realizes multi inheritance.

public interface Incrementable{
void increment();
}

Class myincrement

public class MyIncrement {
public void increment(){
System.out.println("Other increment()");
}
static void f(MyIncrement f){
f.increment();
}
}
public class Callee2 extends MyIncrement implements Incrementable{
public void increment(){
//代码
}
}

I would like to ask you whether the method of increment () belongs to the method that overrides myincrement? Or the incrementable method here. How can I transfer to myincrement? Obviously, it's hard to distinguish. If we use inner classes, we can solve this problem well. Look at the code below

public class Callee2 extends MyIncrement{
private int i=0;
private void incr(){
i++;
System.out.println(i);
}
private class Closure implements Incrementable{
public void increment() {
incr();
}
}
Incrementable getCallbackReference(){
return new Closure();
}
}

We can use the inner class to implement the interface, so that it will not conflict with the methods of the outer class.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>