JAva日常まとめ

80453 ワード

JAvaメソッドが複数の値を返すメソッド
package atguigu1.day08;

public class MethodTest1 {
     
    public static void main(String[] args) {
     
        //              [  ,        , 1.    ,2.   ]
        A a = new A();
        //    
        int[] sumAndMinus = a.getSumAndMinus(1, 4);
        System.out.println("      :" + sumAndMinus[0]);
        System.out.println("      :" + sumAndMinus[1]);

        //  
        MyRes sumAndMinus2 = a.getSumAndMinus2(100, 12);
        System.out.println(" :" + sumAndMinus2.sum);
        System.out.println(" :" + sumAndMinus2.minus);

    }
}
class A{
     
    //         
    //       
    public int[] getSumAndMinus(int a1, int a2){
     
        //    
        int arr[] = new int[2];
        arr[0] = a1 + a2;
        arr[1] = a1 - a2;
        return arr;
    }

    //          
    public MyRes getSumAndMinus2(int a1, int a2){
     
        MyRes myRes = new MyRes();
        myRes.sum = a1 + a2;
        myRes.minus = a1 - a2;
        return myRes;
    }

}
class MyRes{
     
    int sum;
    int minus;
}


楊輝三角

public class  YangHui{
     

	public static void main(String[] args) {
     
		/*           5         <> <>
		1                    
		1 1
		1 2 1
		1 3 3  1
		1 4 6  4  1
		1 5 10 10 5 1
			
			  
			1.     5  
			2.       ,       ,    1  = 1    2    = 2   3   = 3 
			3.       ,                ,    arr[i][j]
			arr[i][j] = arr[i-1][j] + arr[i-1][j-1]; //  

			//  -   
		*/
		
		//      ,           ,     ,       
		int yangHui[][] = new int[5][]; 
		for( int i = 0; i < yangHui.length; i++) {
     //i  
			//   ,    
			yangHui[i] = new int[i+1];
			//   yangHui[i]      
			//       
			for( int j = 0; j < yangHui[i].length; j++) {
     
				
				//                 ,1
				if( j == 0 || j == yangHui[i].length - 1) {
     
					yangHui[i][j] = 1;
				} else {
     
					yangHui[i][j] = yangHui[i-1][j] + yangHui[i-1][j-1];
				}
			}

		}

		//    

		for( int i = 0; i < yangHui.length; i++) {
     //i  
			
			for( int j = 0; j < yangHui[i].length; j++) {
     
				
				System.out.print( yangHui[i][j] + "\t");
			}
			System.out.println();

		}



	}
}


JAvaは方法で配列のコピーを実現する
package atguigu1.day08;

public class MethodTest7 {
     
    public static void main(String[] args) {
     

        //      

        
        MyTools myTools = new MyTools();
        int arr[] = {
     1,2,3,4,5};
        int[] arrNew = myTools.copyArray(arr,2);
        //  arr   arrNew
        myTools.printArr(arr);
        myTools.printArr(arrNew);

    }
}

class MyTools{
     

    //         
    public void printArr(int arr[]){
     
        for (int i = 0; i < arr.length; i++) {
     
            System.out.print(arr[i] + "\t");
        }
        System.out.println();
    }
    public int[] copyArray(int[] arr, int count){
     
        //       
        count = count > arr.length ? arr.length : count;
        //System.out.println(count);

        //      
        int arrNew[] = new int[count];
        //  
        for (int i = 0; i < count; i++) {
     
            arrNew[i] = arr[i];
        }
        return arrNew;
    }
}


クローンオブジェクト、同じアドレスと異なるアドレス
package atguigu1.day08;

public class MethodTest9 {
     
    public static void main(String[] args) {
     
        /*
        3)	      copyPerson,      Person  ,       。    。。,
                             ,         

        //  
        1.      copyPerson
        2.    Person
            Person
         */

        S s = new S();
        //    
        Person p = new Person();
        p.name = "   ";
        p.age = 421;

        Person p1 = p ;//p   p1     s.copyPerson(p); //p p1    

        System.out.println("p1.anme="  + p1.name + " p1.age=" + p1.age);


    }
}
class S{
     
    public Person copyPerson(Person p){
     
        //       Person
        Person person = new Person();
        // person   
        person.name = p.name;
        person.age = p.age;

        return person;

    }
}

class Person{
     
    public String name;
    public int age;
}

JAva instanceofの使用
package com.atguigu.day12;


/*
 * instanceOf      ,              XX   XX       【    】
 */
public class InstanceOfDemo {
     

	public static void main(String[] args) {
     
		Object obj1 = new Object();
		String str1 = new String("Hello");
		
		Taiger taiger = new Taiger();
		
		//   obj1     object      object    
		System.out.println(obj1 instanceof Object); //true
		System.out.println(str1 instanceof Object); //true
		System.out.println(taiger instanceof Object); //true
		System.out.println(taiger instanceof Monster); //true
		System.out.println(obj1 instanceof Taiger); //false
	}

}

class Taiger extends Monster{
     
	
}

class Monster{
     
	
}

マルチステートケース
package com.atguigu.day12;

public class TestFiledMethod {
     

	public static void main(String[] args) {
     
		Sub2 sub = new Sub2();
		System.out.println(sub.count); //20
		sub.display(); //20
		/*
		 *   :
		 *   :     ,     
		 *   :     ,     
		 */
		Base2 s = sub;  //s     
		System.out.println(s.count); //10
		s.display();  //20
		
	}

}

class Base2{
     
	int count =10;
	public void display(){
     
		System.out.println(this.count);
	}
}

class Sub2 extends Base2{
     
	int count = 20;
	public void display(){
     
		System.out.println(this.count);
	}
}

JAvaダイナミックバインドメカニズムの例
package com.atguigu.day12;

public class PolyExam03 {
     

	public static void main(String[] args) {
     
		AAA a = new BBB();  //    
		System.out.println(a.sum()); //40
		System.out.println(a.sum1()); //30 
		
		System.out.println("      ");
		
		System.out.println(a.sum()); 
		// 30      getI  BBB   ,       ,     ,      
		System.out.println(a.sum1()); 
		//20     I        i
	}

}
/*
 * java         
 *           ,                 (    )
 *         ,        ,        。
 */

class AAA {
     
	public int i = 10;

	public int sum() {
     
		return getI() + 10;
	}

	public int sum1() {
     
		return i + 10;
	}

	public int getI() {
     
		return i;
	}

}

class BBB extends AAA {
     

	public int i = 20;

//	public int sum() {//   
//		return i + 20;
//	}

	public int getI() {
     
		return i;
	}

//	public int sum1() {//   ?
//		return i + 10;
//	}

}

マルチステート配列、バブルソート、instanceof、ダウンシフトの総合事例
package com.atguigu.day12;

/*
 *        
 */
public class PolyArr {
     

	public static void main(String[] args) {
     
		Person person[] = new Person[5];
		//     ,                  

		person[0] = new Person("jack", 30);
		person[1] = new Student("smith", 26);
		person[2] = new Student("tom", 20);
		person[3] = new Teacher("joker", 40);
		person[4] = new Teacher("king", 39);
		for (int i = 0; i < person.length; i++) {
     
			System.out.println(person[i]);
		}

		//     
		Person temp = null; //     
		for (int i = 0; i < person.length - 1; i++) {
     
			for (int j = 0; j < person.length - 1 - i; j++) {
     
				//     ,       
				if (person[j].getAge() > person[j + 1].getAge()) {
     
					temp = person[j];
					person[j] = person[j + 1];
					person[j + 1] = temp;
				}
			}
		}
		//         
		System.out.println("========================");
		for (int i = 0; i < person.length; i++) {
     
			System.out.println(person[i]);
		}

		//   persons  ,     study       teach
		//     instanceof              ..
		
		System.out.println("######################");
		for (int i = 0; i < person.length; i++) {
     
			if(person[i] instanceof Student){
     
				//       ,       ,     
				//   
				//    
				((Student)person[i]).study();
			}else if(person[i] instanceof Teacher){
     
				//   
				//    
				Teacher tea = (Teacher)person[i];
				tea.teach();
			}else
				System.out.println("Person ");
		}
	}

}

/*
 *     (    ):          :            Person、Student [2] Teacher[2]  ,
 *            (  ),   say    name age,     .
 */
class Person {
     
	private String name;
	private int age;

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

	public String getName() {
     
		return name;
	}

	public void setName(String name) {
     
		this.name = name;
	}

	public int getAge() {
     
		return age;
	}

	public void setAge(int age) {
     
		this.age = age;
	}

	@Override
	public String toString() {
     
		return "Person [name=" + name + ", age=" + age + "]";
	}

}

class Student extends Person {
     

	public Student(String name, int age) {
     
		super(name, age);
		// TODO Auto-generated constructor stub
	}

	public void study() {
     
		System.out.println("   " + getName() + "     ....");
	}

}

class Teacher extends Person {
     

	public Teacher(String name, int age) {
     
		super(name, age);
		// TODO Auto-generated constructor stub
	}

	public void teach() {
     
		System.out.println("   " + getName() + "     ....");
	}

}

練習問題、問題はコードの中にあります
package com.atguigu.day12;

public class PolyParameter {
     

	public static void main(String[] args) {
     

		Worker3 worker3 = new Worker3("  ", 200, 8);
		
		PolyParameter polyParameter = new PolyParameter();
		polyParameter.showEmpAnnal(worker3);	//     1600.0
		
		Manager3 manager3 = new Manager3("  ", 250, 10, 6000);
		polyParameter.showEmpAnnal(manager3); 
		//        ~
//		     8500.0




		polyParameter.testWork(worker3);//		           ..
		polyParameter.testWork(manager3);//		         ....
		
	}

	//           showEmpAnnal(Employee e),
	//               ,  main        
	//     ,        ,  ,      ,    
	//           
	public void showEmpAnnal(Employee3 e) {
     
		System.out.println(e.getName() + "   " + e.getAnnual());
	}
	//          ,testWork,       ,
		//   work  ,     ,   manage  
		//      【   + instanceof +     】
		public void testWork(Employee3 e) {
     
			//   ,     ,     
			if(e instanceof Worker3) {
     
				//    
				((Worker3)e).work();
			} else if(e instanceof Manager3) {
     
				//    
				((Manager3)e).manage();
			} else {
     
				System.out.println("   ");
			}
		}


}

/*
 *     2: •      Employee3,        [private],
 *        getAnnual   。    (Worker3)   (Manager3)     ,
 *        bonus     manage  ,        work  ,               getAnnual  
 * 
 * 
 */
class Employee3 {
     
	private String name;
	private double monSal; //    
	private int salMon; //     

	public Employee3(String name, double monSal, int salMon) {
     
		super();
		this.name = name;
		this.monSal = monSal;
		this.salMon = salMon;
	}

	public String getName() {
     
		return name;
	}

	public void setName(String name) {
     
		this.name = name;
	}

	public double getMonSal() {
     
		return monSal;
	}

	public void setMonSal(double monSal) {
     
		this.monSal = monSal;
	}

	public int getSalMon() {
     
		return salMon;
	}

	public void setSalMon(int salMon) {
     
		this.salMon = salMon;
	}

	public double getAnnual() {
     
		return monSal * salMon;
	}

}

class Worker3 extends Employee3 {
     

	public Worker3(String name, double monSal, int salMon) {
     
		super(name, monSal, salMon);
		// TODO Auto-generated constructor stub
	}

	//     
	public void work() {
     
		System.out.println("    " + getName() + "     ..");
	}

	//   getAnnual   
	@Override
	public double getAnnual() {
     
		// TODO Auto-generated method stub
		return super.getAnnual();
	}
}

class Manager3 extends Employee3 {
     
	private double bonus; //   

	public Manager3(String name, double monSal, int salMon, double bonus) {
     
		super(name, monSal, salMon);
		this.bonus = bonus;
	}

	public double getBonus() {
     
		return bonus;
	}

	public void setBonus(double bonus) {
     
		this.bonus = bonus;
	}

	//      
	public void manage() {
     
		System.out.println("   " + getName() + "    ....");
	}

	//   getAnnual   
	@Override
	public double getAnnual() {
     

		//       ,    bonus ,      
		System.out.println("        ~");
		return super.getAnnual() + bonus;
	}

}