Object oriented day 3

inherit

Inheritance:

1.extends

2. Rewrite (two are the same, two are small and one is large)

3.super keyword (this)

super:

  • -Call the member variable of the parent class
  • -Call the instance method of the parent class
  • -Call the constructor of the parent class

4. (key) if no construction method is added manually in a class, a default parameterless construction will be provided

If you manually add a construction method to a class, the default parameterless construction will not be provided

The first sentence of the subclass's constructor must hide a super() calling the parameterless constructor of the parent class

If the constructor of the parent class (no parameter or parameter) is manually called in the constructor of the child class, the parameterless constructor of the parent class will not be called by default in the first sentence

Note: like this, when we call the constructor of the parent class, we must only call it on the first line of the code

this() must be on the first line when calling the constructor

When super() is called, it must also be in the first line

Note: this and super cannot appear simultaneously in the same method of a subclass

5. Characteristics of inheritance

  • Inheritance is transitive

For example: Boo extensions Aoo Coo extensions boo, then Coo has the public attributes and behaviors in Aoo

  • After inheritance, the child class has all the common properties and behaviors in the parent class

  • A class can only inherit from a single class (a class can only inherit from one class)

Aoo

/**
 * Object:Is the parent of all reference types
 * 
 *
 */
public class Aoo {
     public int a;
     public int b;
     
     public Aoo() {
    	 System.out.println("Aoo Nonparametric structure of");
     }
     public Aoo(int a,int b) {
    	 this.a=a;
    	 this.b=b;
    	 System.out.println("Aoo Parametric structure of");
     }
	@Override
	public String toString() {
		return "Aoo [a=" + a + ", b=" + b + "]";
	}
     
}

Boo

public class Boo extends Aoo{
	public int c;
       public Boo() {
    	   super(10,20);
    	   System.out.println("Boo Nonparametric structure of");
       }
       public Boo(int c) {
    	   this();
    	   this.c=c;
    	   System.out.println("Boo Parametric structure of");
       }
}

Coo

public class Coo extends Boo{

}

Demo1

public class Demo1 {
    public static void main(String[] args) {
		/*Aoo aoo=new Boo(10);
		Aoo app=new Boo();*/
    	//The toString method is called by default
    	Aoo aoo=new Boo();
    	Aoo aoo2=new Coo();
    	/**
    	 * Because we want to cast the aoo2 variable to Boo type, we need to judge
    	 * Judge whether the object referred to by aoo2 variable is a subclass object of Boo class or this class object
    	 */
    	if(aoo2 instanceof Boo) {//A instanceof B
    		System.out.println("true");
    		Boo boo=(Boo)aoo2;//Boo boo=new Coo();
    	}
    	
    	Boo boo=(Boo)aoo2;
    }
}

6. Downward modeling: (conversion type)

instanceof: a boolean value will be returned

For example: A instanceof B

If you want to cast A variable to type B, you need the above conditions to judge

ClassCastException//Type conversion exception
NullPointerException//Null pointer exception
ArrayIndexOutOfBoundsException//Subscript out of bounds

Judge whether the object referred to by A variable is A subclass of B or an object of this class

If yes, it returns true; if not, it returns false

Note: generally, during downward modeling, instanceof judgment must be carried out before forced conversion, otherwise an exception will be thrown

often

TuxXing

/**
 * Create a graphics class
 * 
 *
 */
public class TuxXing {
     public int c;
	/*public double area(){
		return 0.0;
	}*/
}

YuanXing

/**
 * Circular class
 * Area calculation formula of assumed circle:
 * Perimeter * perimeter * perimeter * 0.0258
 *
 */
public class YuanXing extends TuxXing{
     public int c;//Perimeter

	public YuanXing(int c) {
		super();
		this.c = c;
	}
     
	/**
	 * Area calculation formula of circular
	 */
	
	public double area2() {
		return c*c*c*0.0258;
	}
}

FangXing

/**
 * Square class
 * It is assumed that the calculation formula of all square areas is:
 * Perimeter * perimeter * perimeter * 0.052
 *
 */
public class FangXing extends TuxXing{
        public int c;//Perimeter
        
        
        public FangXing(int c) {
			this.c = c;
		}


		/**
         * Method of calculating area in square class
         * 
         */
        public double area1() {
        	return c*c*c*0.052;
        }
}

Test

public class Test {
	  public static void main(String[] args) {
		/**
		 * It is known that the perimeter of the three squares is 5, 6 and 7 respectively
		 * What is the largest area of the three squares?
		 * 
		 * It is known that the circumference of several circles are 11, 12, 13, 14 and 15 respectively
		 * What is the maximum area of the circle?
		 */
		/* FangXing [] fx=new FangXing[5];
		 fx[0]=new FangXing(5);
		 fx[1]=new FangXing(6);
		 fx[2]=new FangXing(7);
		 fx[3]=new FangXing(8);
		 fx[4]=new FangXing(9);
		//Assume that the area of the first square in the array is the maximum
		 double max=fx[0].area();
		 for(int i=0;i<fx.length;i++) {
		  double d=fx[i].area();
		  if(max<d) {
			  max=d;
		  }		  
		 }
		 System.out.println(max);
		 
		 
		 YuanXing [] yx = new YuanXing[5];
		 yx[0]=new YuanXing(11);
		 yx[1]=new YuanXing(12);
		 yx[2]=new YuanXing(13);
		 yx[3]=new YuanXing(14);
		 yx[4]=new YuanXing(15);
		 double ymax=yx[0].area();
		 for(int i=0;i<yx.length;i++) {
		  double y=yx[i].area();
		  if(ymax<y) {
			  ymax=y;
		  }
		 }
		 System.out.println(ymax);
		 
		 if(max<ymax) {
		  System.out.println("The maximum area is "+ ymax);
		 }else {
		  System.out.println("The maximum area is "+ max);
		 }*/
		  
		  TuxXing [] tx=new TuxXing[10];
		  tx[0]=new FangXing(5);
		  tx[1]=new FangXing(6);
		  tx[2]=new FangXing(7);
		  tx[3]=new FangXing(8);
		  tx[4]=new FangXing(9);
		  tx[5]=new YuanXing(11);
		  tx[6]=new YuanXing(12);
		  tx[7]=new YuanXing(13);
		  tx[8]=new YuanXing(14);
		  tx[9]=new YuanXing(15);
		  
		  //double max=tx[0].area();
		  double max=0;
		  for(int i=0;i<tx.length;i++) {
			  double d=0;
			  if(tx[i] instanceof FangXing) {
				  FangXing fx=(FangXing)tx[i];
				  d=fx.area1();
			  }
			  if(tx[i] instanceof YuanXing) {
				  YuanXing fx=(YuanXing)tx[i];
				  d=fx.area2();
			  }
			  //double d=tx[i].area();
			  if(max<d) {
				  max=d;
			  }
		  }
		  System.out.println("The maximum area of all drawings is:"+max);
	}
}

Tetris concept case

cell

 */
public class Cell {
    public int x;//Represents the x coordinate
    public int y;//Represents the y coordinate
	public Cell(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}
	
	@Override
	public String toString() {
		return "(" + x + "," + y + ")";
	}
    
    

Test2

package day10;

public class Test2 {
	public static void main(String[] args) {
		/*Cell cell=new Cell(1,2);
		Cell cell2=new Cell(2,2);
		Cell cell3=new Cell(3,2);
		Cell cell4=new Cell(2,3);*/
		Cell[] c=new Cell[4];
		c[0]=new Cell(1,2);
		c[1]=new Cell(2,2);
		c[2]=new Cell(3,2);
		c[3]=new Cell(2,3);
		printCell(c);
		//It is known that the coordinates of a point in the L graph are (2,2)
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		Cell[] c2=new Cell[5];
		c2[0]=new Cell(2,2);
		c2[1]=new Cell(2,3);
		c2[2]=new Cell(2,4);
		c2[3]=new Cell(3,4);
		c2[4]=new Cell(4,4);
		printCell(c2);
		//System.out.println(cell);
		/*
		 * Print game background wall 10 * 20
		 * -------
		 * --***--
		 * ---*---
		 * 
		 * Print another drawing of L
		 * -------
		 * --*----
		 * --*----
		 * --***--
		 */
		/*for(int i=0;i<10;i++){//Represents a row (y coordinate value)
			for(int j=0;j<20;j++){//Represents the column (x coordinate value)
				if((i==cell.y && j==cell.x) ||
					(i==cell2.y && j==cell2.x)||
					(i==2 && j==3)||
					(i==3 && j==2)){
					System.out.print("*");
				}else{
					System.out.print("-");
				}
			}
			System.out.println();
		}*/
	}
	public static void printCell(Cell[] cell){
		for(int i=0;i<10;i++){//Represents a row (y coordinate value)
			for(int j=0;j<20;j++){//Represents the column (x coordinate value)
				//Define the switch. Print only if * is not printed-
				boolean flag=false;
				//Traverse the cell array and find the point coordinates inside
				for(int k=0;k<cell.length;k++){
					//Determine whether each point in the array is found
					if((i==cell[k].y && j==cell[k].x)){
						    //Print when you find it
							System.out.print("*");
							//Change the switch status
							flag=true;
					}
				}
				//First judge whether the switch has been changed. If it has not been changed, print it-
				if(!flag){
					System.out.print("-");
				}
			}
			System.out.println();
		}
		
	}
	
}

​ 2021-07-09 19:34

Keywords: Java

Added by joespenceley on Sat, 22 Jan 2022 21:38:52 +0200