자바 제4 절 류 의 계승 / 추상 / 인터페이스 / 다 형 성

11084 단어 자바
/*
    
            
java      ,       
       ,               ,  B    A, C      B
   C       A

                  ,           ,
              super(    )
         

                       ,     this              ,            
                

          

1                     ,   new        
                3.1                     

2         ,  new Person(      )                      

3    this()  ,            (              2        )
                    ,        ,             6  

4                 (   Ojbect   ,Object   java       )
              ,           2           ,               ,        ,            

5               ,                       

6                   

1):   super() this()                  ?
                 this()    ,                      super()  
           ,              super()   ,         ,   
             


2):   super() this()                  
              ,       
              ,    this super()  
                  
                this,    this  super()  
             


       
                        ,          

                       ,     super     

        ,                      

   
final   
   1  java    ,      ,       final   
   2 final         
   3 final            
   4 final     (         )     ,      
   5                   final       
      final               ,                  
	         final     ,                    
   6 public static final       ,           

   
   java               ,                         
              ,             

   1        abstract      ,        abstract   
   2          ,      new        
   3        ,     
   4                 ,                       ,            

*/

//final class Person        
class Person
{
	//protected final String name = "unknown";    final          
	protected String name = "unknown";


	//public final String x = "abc"; //          

	public  final String x;
    //public static final String x;

	public static final String y = "abc";




	public int age = -1;

	public Person()
	{
	    this.x = "cde";
	}

    public Person(String name, int age)
	{
	    this.name = name;
		this.age = age;
		//            
		this.x = "abc";

	}

    //public void getInfo(){}          
	public void getInfo()
	{
		//this.x = "abc"; // final            
	    System.out.println("name:"+name+", age:"+age);
	}
};


class Student extends Person
{

	public String school = "unknown";

	public Student()
	{
	    //super("xlc",15);
		super();
	}

	public Student(String name, int age, String school)
	{
	   
	    this(name,age); //         
		this.school = school;
		//this(name,age); //         
	}

	public Student(String name, int age)
	{
	     super(name, age);
	}

	public void getInfo()
	{
	     System.out.println("school:"+school+"name:"+name+", age:"+age);
		 super.getInfo();
	}

	public void study()
	{
	
	}
};

class TestStudent
{
	public static void main(String[] args)
	{
	    
		//Student st = new Student();
		//st.name = "xlc";
		//st.age = 34;
		//st.getInfo();

		Student st = new Student("xlc",22,"    ");
		st.getInfo();

		System.out.println(Float.MAX_VALUE);
	}
};

  
/*
  (interface)
                  ,                   ,       
                 ,     ,           
                 ,           

1         public     ,         public static final   

2              extends              

3            implements                ,              implements                  

4               ,         ,extneds       implements     


       
   1              
   2                
   3 instanceof                       
   4 Object  equals  


*/
/*abstract class A
{
	abstract int aa(int x, int y);

    //         


};


class B extends A
{
     int aa(int x, int y)
	 {
	 
	     return 1;
	 }
};*/


interface Runner
{
    int ID = 1;

	void run();
}

interface Animal extends Runner
{
     void breathe();
}


class Fish implements Animal
{
     public void run()
	 {
	     System.out.println("fish is swimming");
	 }

	 //public void breathe()
	 public void breathe()
	 {
	     System.out.println("fish is bubbling");
	 }

	 public static void main(String[] args)
	 {
	      Fish f = new Fish();
		  int j=0;
		  j = Runner.ID;
		  j = f.ID;
		  //f.ID = 2; //       

	 }
};

abstract class LandAnimal implements Animal
{
	public void breathe()
	{
	
	}
};



interface dog
{
     void uaau();
}

interface Flyer
{
     void fly();
}

class Bird implements Runner, Flyer
{
	public void run()
	{
	
	}

	public void fly()
	{
	
	}
};




//      ,        
//            
class Student extends Person implements Runner, dog
//        ,extends   implements  
{
	
	public void run()
	{
	
	}

	public void uaau()
	{
	
	}
};


interface A 
{
	//public static final int ID=1;
	//int ID = 1; //              ,        ,      ,
	//          

	public static final int ID=1;

    int aa(int x, int y);

	void bb();
}






/*class  
{
	public static void main(String[] args) 
	{
		System.out.println("Hello World!");
	}
}*/

  
/*
        
1):             (  )      
              ,    "      ",             

2):                  ,      ,               
                       ,      ( callA  )         (        A   ,  D)     


*/
class A
{
	public void func1()
	{
	    System.out.println("A func1 is calling");
	}

	public void func2()
	{
	    func1();
	}
};

class B extends A
{
	public void func1()
	{
	    System.out.println("B func1 is calling");
	}

	public void func3()
	{
	    System.out.println("B func3 is calling");
	}
};

class C
{
	public static void main(String[] args)
	{
	    
		B b = new B();
		callA(b);

		//A aa = b;
		//callA(new A());
	}

	public static void callA(A a)
	{
		/*if(a instanceof B)
		{
		  B b = (B)a; //      
		  b.func1();
		  b.func2();
		  b.func3();
		}else{
		  a.func1();
		  a.func2();
		}*/


		a.func1();
		a.func2();

		//a.func1();
		//a.func2();
		//a.func3();
	}
};


class Student extends Object
{

	 private String name;

	 private int age;

	 public Student(String name, int age)
	 {
	     this.name = name;
		 this.age = age;
	 }


     /*public boolean equals(Object obj)
	 {
	      Student st = null;
		  if(obj instanceof Student)
		  {
		       st = (Student)obj;
			   if(st.name == name && st.age == age)
			   {
			      return true;
			   } else{
			      return false;
			   }
		  }else{
		       return false;
		  }
	 }*/
	 //         equals  ,       
	 //         

	 public static void main(String[] args)
	 {
	     Student st1 = new Student("  ",20);
		 Student st2 = new Student("  ",20);
		 if(st1.equals(st2))
		 {
		     System.out.println("  ");
		 }else{
		     System.out.println("   ");
		 }
	 }

};



/*class  
{
	public static void main(String[] args) 
	{
		System.out.println("Hello World!");
	}
}*/

  
interface PCI
{
   void start();

   void stop();

}

class NetWrokCard implements PCI
{
    public void start()
	{
	    System.out.println("Send...");
	}

	public void stop()
	{
	    System.out.println("stop...");
	}
};


class SoundCard implements PCI
{
	public void start()
	{
	    System.out.println("du...");
	}

	public void stop()
	{
	    System.out.println("sound stop...");  
	}
};

class MainBoard
{
	public void usePCICard(PCI p)
	{
		p.start();
		p.stop();
	}
};

class Assembler
{

	public static void main(String[] args)
	{
	     MainBoard mb = new MainBoard();

		 NetWrokCard nc =  new NetWrokCard();

		 SoundCard sc = new SoundCard();

		 mb.usePCICard(nc);
		 mb.usePCICard(sc);

		 //   
		 /*mb.usePCICard(
		   new PCI()
		   {
		        public void start()
				{
				   System.out.println("test start...");
				} 

				public void stop()
				{
				   System.out.println("test end...");
				}
		   }			 
		 );*/

		 //   
		 class A implements PCI
		 {		       
			    public void start()
				{
				   System.out.println("test start...");
				} 

				public void stop()
				{
				   System.out.println("test end...");
				}

		 };
		 mb.usePCICard(new A());

	}
};

좋은 웹페이지 즐겨찾기