java study notes 8: Method return type and argument types, inner classes, anonymous inner classes

First, a method return type and argument types
1, in the form of parameters
① To a basic type of a basic type of variable or constant values specific
② time to the class name of an object class
③ abstract class name to is a class inherited from a subclass object time
④ is time to interface is the interface implements a subclass object
2, the return type
① is the basic data type is returned when a specific value of
② is the basic data type is returned when a specific value
③ abstract class name should be returned when a subclass object inherits from class
④ is returned when the interface name is an implementation of the interface subclass object

传参
public class MyTest {
    public static void main(String[] args) {
        Student student = new Student();
        student.setStudent(new Student(),"张三");
        Cat cat = new Cat();
        Animal an = new Cat();
        setAnimal(an);
        System.out.println("---------------");
        setInterface(new MyClass());
    }
    public static void setAnimal(Animal an) {
        an.eat();
    }   
    public static void setInterface(MyInterface myInterface) {
        myInterface.test();
    }
}
interface MyInterface {
void test();
}
class MyClass implements MyInterface{
    @Override
    public void test() {
      System.out.println("我重写了接口的方法");
    }
}
class Student {
    String name;
    public void setStudent(Student s,String mz){
        s.name=mz;
    }
}

return value

  public static Animal getAnimal(){
          return new Dog();
        }
        public static MyInterface getInterface(){     
            return new MyClass();
        }
        public Teacher getTeacher(Teacher teacher,String mz){
        teacher.name=mz;
        return this;
    }

Second, internal class
to class definition within the other classes, the class known as inner classes.
1, features ::
① inner class can directly access the external members of the class, including private.
② To access the outer class members within the class, you must create the object.
2, according to the definition of the class into the interior position, members of the class and the local internal inner classes

class B{
    class A{} //成员内部类
    public void  show(){
        class C{}//局部内部类
    }
}

Access class members inside and outside the internal format: class name inside the external name class object name = internal external object class object;.

class Outer {
	public int num = 10;
	class Inner {
		public int num = 20;
		public void show() {
			int num = 30;
			System.out.println(num);//30 
			System.out.println(Inner.this.num);//20
			System.out.println(Outer.this.num);//10 
		}
	}
}
	class InnerClassTest {
		public static void main(String[] args) {
			Outer.Inner oi = new Outer().new Inner();
			oi.show();
		}	
	}

Local inner class access local variables:
① access to members outside of class direct
② can create an internal class object by object calls an internal class method to use a local inner class function
③ local inner class access local variables must be modified by the final; because local variables with the method call is completed will disappear, this time, local objects are not immediately disappear from the heap memory, but also to use that variable. To make the data can continue to be used, use fianl modified so that, in fact, is a constant value in the heap memory stored inside.

class Outer { 	
	// 成员变量
	private int a = 45 ;   	
	// 成员方法
	public void show() { 
		final int b = 45 ;//JDK1.7之前要加final不然会报错  但是在JDK1.8 不加不会报错
		class Inner {
			public void method(){
				System.out.println(a) ;//45
				System.out.println(b) ;//45
			}
		}
		Inner i = new Inner() ;
		i.method() ;    	
	}
}    
class InnerClassDemo6 { 
	public static void main(String[] args) { 		
		Outer out = new Outer() ;   
		out.show() ;
	}
}

Third, anonymous inner classes
1 is a simplified partial wording within the class.
2, with the proviso: the presence of a class or interface; herein may be a particular class may be abstract class.
3, format:

new 类名或者接口名(){
			重写方法;
		} ;

4, is essentially a inherited class or subclass that implements this interface anonymous object.

public class MyTest {
    public static void main(String[] args) {
        //匿名内部类
         setFu(new Fu() {
             @Override
             public void show() {
                 System.out.println("我重写了show方法");
             }
         });
        Zi zi = new Zi();
        setFu(zi);
    }
       public static void setFu(Fu f){
    f.show();
  }
}
  abstract class Fu {
     public abstract void show();
  }
class Zi extends Fu{
    @Override
    public void show() {
        System.out.println("我重写了父类的show方法");
    }
 }
//输出:我重写了show方法
     //我重写了父类的show方法
Published 24 original articles · won praise 11 · views 2060

Guess you like

Origin blog.csdn.net/weixin_43791069/article/details/85241161