java learning notes

QUESTION:

1. Use debug to track the order in which subclass objects are instantiated:

package lianxi;
//Subclass
public class abc extends ab{
	int a=0,b=0,c=0;
	abc(int x)
	{
		super(x); a=x+7;
	}
	abc(int x,int y){
		super(x,y);a=x+5;b=y+5;
	}
	abc(int x,int y,int z){
		super(x,y,z);a=x+4;b=y+4;c=z+4;
	}
	public int add()
	{
		System.out.println("super:x+y+z="+super.add());
		return a+b+c;
	}
	public static void main(String[] args) {
		abc p1=new abc(2,3,5);
		abc p2=new abc(10,20);
		abc p3=new abc(1);
		System.out.println("a+b+c="+p1.add());
		System.out.println("a+b="+p2.add());
		System.out.println("a="+p3.add());
	}
}

package lianxi;
//Parent Class
public class ab {
	private int x=0,y=0,z=0;
	ab(int x){
		this.x=x;
	}
	ab(int x,int y){
		this(x);
		this.y=y;
	}
	ab(int x, int y,int z){
		this(x,y);
		this.z=z;
	}
	public int add() {
		return x+y+z;
	}
}

Now debug the subclasses to verify the order in which they are instantiated.
1.abc p1=new abc(2,3,5);
2.abc p2=new abc(10,20);
3.abc p3=new abc(1);
Statement 1 is as follows:
(1)

(2)

(3)

(4)

(5)

(6)

The assignment is then made. The debug order of the 2,3 statements is similar.
First enter the constructor of the subclass, then enter the parent constructor before assigning, then skip to the previous constructor continuously due to overloading, and finally enter the final parent object, and then do the assignment operation after that.

2. How to exchange messages between two objects:

You can create two classes by means of object references, both of which contain members of another class.

package lianxi;

class A{
	private abc b;
	public void bb(abc b){
		if(b!=null){
			System.out.println("ooo");
			this.b=b;
		}
	}
	public abc get(){
		
		if(b!=null){
			return b;
		}
		else 
			return null;
	}
}
class abc{
	private A a;
	
	public abc(A a){
		this.a=a;
		a.bb(this);
	}
	public static void main(String[] args) {
		A a=new A();
		abc bb = new abc(a);
	}
}

Output:

3. Differences between combinations and inheritance and usage scenarios:

Difference: Combinatorial phenomena is that an object reference of one class is an attribute of another class. An object reference can point to any object of its corresponding class, but an object can only call its specified method and cannot be modified. Inheritance is that a subclass inherits all the attributes and methods of the parent class. A subclass can overload and override the methods of the parent class, and inheritance has properties similar to one-to-one.

Scenarios where combinations are appropriate: objects of one class have a one-to-many relationship with objects of another class, and the methods of one class never change for another class.

Inheritance scenarios are appropriate: objects of two classes have a one-to-one relationship, and one class needs to overload or override the methods of another class; Abstract classes and so on.

4. The meaning and function of Java runtime polymorphism:

Runtime polymorphism: When the same reference variable points to different subclass instances and then accesses the reference variable member methods, the methods behave differently. Different subclasses show different results when using parent references to point to subclass objects and then calling methods in a parent class.
Role: Runtime polymorphisms provide greater flexibility and everything is resolved at runtime.

package lianxi;

abstract public class Shapes {
	protected int x,y,k;
	protected double m;
	
	public Shapes(int x,int y,int k,double m) {
		this.x=x;
		this.y=y;
		this.k=k;
		this.m=m;
	}
	
	abstract public double getArea();
	abstract public double getPerimeter();
}
package lianxi;
import java.awt.*;

public class Rect extends Shapes{
	public double getArea() {
		return k*m;
	}
	
	public double getPerimeter() {
		return (2*k+2*m);
	}
	
	public Rect(int x,int y,int width,int height) {
		super(x,y,width,height);
	}
}

package lianxi;
import java.awt.*;

public class Triangle extends Shapes{
	public Triangle(int baseA,int baseB,int baseC) {
		super(baseA,baseB,baseC,0);
		m=(baseA+baseB+baseC)/2.0;
	}
	
	public double getArea() {
		return(Math.sqrt(m*(m-k)*(m-x)*(m-y)));
	}
	
	public double getPerimeter() {
		return (x+y+k);
	}
}

package lianxi;
import java.awt.*;

public class Circle extends Shapes{
	public Circle(int x,int y,int width) {
		super(x,y,width,width/2.0);
	}

	public double getArea() {
		return m*m*Math.PI;
	}
	
	public double getPerimeter() {
		return 2*Math.PI*m;
	}
}

package lianxi;
import java.awt.*;
import java.applet.*;

public class RunShape extends Applet {
	Rect rect=new Rect(5,15,25,25);
	Triangle tri =new Triangle(5,5,8);
	Circle cir=new Circle(13,90,25);
	
	private void drawArea(Graphics g,Shapes s,int a,int b) {
		g.drawString(s.getClass().getName()+" Area"+s.getArea(),a,b);
	}
	
	private void drawPerimeter(Graphics g,Shapes s,int a,int b) {
		g.drawString(s.getClass().getName()+"Perimeter"+s.getPerimeter(),a,b);
	}
	
	public void paint(Graphics g) {
		g.drawRect(rect.x, rect.y, rect.k, (int)rect.m);
		drawArea(g,rect,50,35);
		drawPerimeter(g,rect,50,55);
		drawArea(g,tri,50,75);
		drawPerimeter(g,tri,50,95);
		g.drawOval(cir.x-(int)cir.k/2,cir.y-(int)cir.k/2,cir.k,cir.k);
		drawArea(g,cir,50,115);
		drawPerimeter(g,cir,50,135);
	}
}


5. Rewrite 6.8 using the interface:

public interface Shapes {
	public abstract double getArea();
	public abstract double getPerimeter();
}
public class Rect implements Shapes{
	int x,y;
	double width,height;
	public Rect(int x,int y,double width,double height) {
		this.x=x;
		this.y=y;
		this.width=width;
		this.height=height;
	}
	
	public double getArea() {
		return width*height;
	}
	
	public double getPerimeter() {
		return 2*(width+height);
	}
}

public class Triangle implements Shapes {
	int baseA,baseB,baseC;
	double m;
	
	public Triangle(int x,int y,int z) {
		baseA=x;
		baseB=y;
		baseC=z;
		m=(baseA+baseB+baseC)/2.0;
	}
	
	public double getArea() {
		return (Math.sqrt(m*(m-baseA)*(m-baseB)*(m-baseC)));
	}
	
	public double getPerimeter() {
		return (double)(baseA+baseB+baseC);
	}
}

public class Circle implements Shapes{
	int x,y;
	double d,r;
	public Circle(int x,int y,int width) {
		this.x=x;
		this.y=y;
		r=width/2.0;
		d=(double)width;
	}
	
	public double getArea() {
		return (r*r*Math.PI);
	}
	
	public double getPerimeter() {
		return (d*Math.PI);
	}
}

import java.applet.*;
import java.awt.*;

public class RunShape extends Applet {
	Rect rect=new Rect(5,15,25,25);
	Triangle tri=new Triangle(5,5,8);
	Circle cir=new Circle(13,90,25);
	
	private void drawArea(Graphics g,Shapes s,int a,int b) {
		g.drawString(s.getClass().getName()+"Area"+s.getArea(), a,b);
	}
	
	private void drawPerimeter(Graphics g,Shapes s,int a,int b) {
		g.drawString(s.getClass().getName()+"Perimeter"+s.getPerimeter(),a,b);
	}
	
	public void paint(Graphics g) {
		g.drawRect(rect.x, rect.y,(int)rect.width, (int)rect.height);
		drawArea(g,rect, 50, 35);
		drawPerimeter(g,rect,50,55);
		drawArea(g,tri,50,75);
		drawPerimeter(g,tri,50,95);
		g.drawOval(cir.x-(int)cir.r,cir.y-(int)cir.r ,(int)cir.d,(int)cir.d);
		drawArea(g,cir,50,115);
		drawPerimeter(g,cir,50,135);
	}
}

6. Custom class, override equals:

class Man{
    private String name;
    private int age;
    Man(String name,int age){
        this.name=name;
        this.age=age;
    }
    public String toString(){
        return this.name+"This year"+this.age+"year";
    }
    public boolean equals(Object obj){//Object classes can accept any class
        if(obj==null){//Determines if it is null or if it is not, a NullPointerException will occur
            return false;
        }
        if(this==obj){//Determine if you are comparing with yourself (by comparing addresses), and if so return true directly
            return true;
        }
        if(!(obj instanceof Man)){//instanceof is used to determine if the object to the left is an instance of the object to the right, and here is to determine if the object in the parentheses of the equals() method in the main method is a Person class
            return false;
        }
        //Arriving here must be a comparison of objects of the same type but with different addresses
        Man per=(Man)obj;//Downward transition, comparing attribute values
        return this.name.equals(per.name)&&this.age==per.age;//Determine if attribute content is equal (error prone)
    }
}
class Student{}
public class Test{
    public static void main(String[] args) {
       Person per1=new Man("Zhang San",18);
       Person per2=new Man("Zhang San",18);
       Person per3=new Man("lisi",19);
       Person per4=null;
       Student stu=new Student();
       System.out.println(per1.equals(per1));//true
       System.out.println(per1.equals(stu));//false
       System.out.println(per1.equals(per3));//false
       System.out.println(per1.equals(per4));//false
    }
}

7.instanceof usage scenarios:

Used to determine whether there is a parent-child relationship between the two classes.
Lift a chestnut:
A is an object reference and A is a class. A is an instance of A or a subclass of A, returning true.
A is a parent class instance of A, returning false.
A has no relationship with A and compilation fails.

8. Similarities and differences between abstract classes and interfaces, and usage scenarios:

Same:
Both are abstract classes and cannot be instantiated.
Both the interface implementation class and the subclasses of abstract class must implement the declared abstract methods.

Keywords: Java

Added by bagpuss03 on Mon, 18 Oct 2021 19:51:23 +0300