Java基础-接口,内部类及对象克隆的学习

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wanlong360599336/article/details/59583242

1.效果图:


2.代码:

public class InterfaceStudy {

	//一个类可以实现多个接口,但只能继承一个抽象类
	//抽象类的方法修饰符可以为public,protect,private也可以是默认的,但是接口的方法全部是public
	//抽象类有构造函数,而接口没有,且都不能实例化,但都能实现类的对象,具有多态化
	public static void main(String[] args) {
		System.out.println("-----------接口的使用----------");
		AllObjects mouse=new Mouse();
		AllObjects cat=new Cat();
		mouse.output();
		//要想调用Mouse里面的方法,必须转换类型,否则只能调用AllObjects接口所定义的方法
		((Mouse)mouse).Export();
		cat.output();
		
		//AllObjects mouse=new Mouse();和Airport aport=new Mouse();表示接口的多态化
		Airport aport=new Mouse();
		aport.Export();
		aport.output();
		
		System.out.println("-----------内部类的使用----------");
		OutClass oClass=new OutClass();
		oClass.outPrint();
		
		//宿主类或其对象可以直接调用静态类去实例化,不能直接调用非静态去操作
		//OutClass.InnerClass ic=new OutClass.InnerClass();
		OutClass.InnerClass1 ic2=new OutClass.InnerClass1();
		ic2.ExportMethod();
		
		System.out.println("-----------对象克隆----------");
		Human hman=new Human("Lily",20,"钵兰街49号");
		hman.Info();
		
		//Human hmanCopy=hman;实际上是hmanCopy指向了hman所指向的对象,即为同一个对象;当hmanCopy的值改变了,其hman的值也跟着变了
		//为了防止源对象的值跟着发生改变,则引用了clone
		Human hmanCopy=(Human)hman.clone();
		hmanCopy.Name="Lucy";
		hmanCopy.Age=25;
		hmanCopy.Address="香港路36号";
		hmanCopy.Info();
		hman.Info();
		
	}
}

interface AllObjects
{
	//接口中变量的实际效果是public static final int bj=1;
	int bj=1;
	void output();
}

interface Airport extends AllObjects
{
	void Export();
}

//如果子接口继承了父接口,实现了子接口的方法,也必须实现父接口所有的方法
class Mouse implements Airport
{
   public void Export()
   {
        //实现接口,可以直接调用接口里面的变量
	    System.out.println(bj);
   }
	
   public void output()
   {
	    System.out.println("output Mouse");
   }
}

class Cat implements AllObjects
{
	public void output()
	{
		System.out.println("output Cat");
	}
}

//内部类的使用
class OutClass
{
	String strings="outString";
	void outPrint()
	{
		InnerClass ic=new InnerClass();
		ic.print();
		//非静态内部类里面的变量和方法,需实例化出对象进行访问;
		//静态内部类里面的变量和方法若定义为static,则可以直接用类名访问,若没有,则用对象进行访问
		InnerClass1 ic1=new InnerClass1();
		System.out.println(ic.noStaticData+"------"+InnerClass1.staticString);
		//注意宿主和内部类之间的互相调用,使用时千万别出现死循环
		ic1.ExportMethod();
	}
	//非静态内部类
    class InnerClass
	{
    	String noStaticData="noStaticData";
		void print()
		{
			//非静态内部类可以直接调用宿主类里的变量和方法
			System.out.println("export data:"+strings);
			System.out.println("InnerClass");
		}
	}
    //静态内部类
    static class InnerClass1
    {
    	static String staticString="staticData";
    	void print()
		{
    		//System.out.println("export data:"+strings);
        	//不能直接调用宿主变量,必须定义使用对象类访问
        	OutClass occ=new OutClass();
        	occ.outPrint();
    		System.out.println("export data:"+occ.strings);
		}
    	
    	void ExportMethod()
    	{
    		System.out.println("export data2");
    	}
    }
}

//实现类的克隆,必须要实现Cloneable接口,并将Clone方法定义为public
class Human implements Cloneable
{
	String Name;
	int Age;
	String Address;
	Human(String name,int age,String address)
	{
		this.Name=name;
		this.Age=age;
		this.Address=address;
	}
	
	void Info()
	{
		System.out.println("Name:"+this.Name);
		System.out.println("Age:"+this.Age);
		System.out.println("Address"+this.Address);
	}
	
	//对象克隆的方法
	public Object clone()
	{
		Human human=null;
		try
		{
			human=(Human)super.clone();
		}
		catch(CloneNotSupportedException e)
		{
			e.printStackTrace();
		}
		return human;
	}
}


猜你喜欢

转载自blog.csdn.net/wanlong360599336/article/details/59583242