Java introductory study notes (5)-simple class and object, class definition, object creation, construction method, this keyword, initialization of object member properties, static modifier, variable scope

1. Preface

In java, there is a lot of knowledge about classes, here is only a brief description of the basic knowledge.

Two, class definition and creation of objects

Parent class-super class
specifies the subclass of the parent class-derived class

When creating an object, you need to define the corresponding construction method

Three, construction method

The construction method has no return value type
(if the return value type is given, Java will treat this so-called construction method as a general method)

Exercise 1-rectangular class

package try_a_package;

public class Rectangle {
    
    
	private double a;
	private double b;
	private String name="矩形";
	
	public Rectangle()
	{
    
    
		a=1;
		b=2;
		name="矩形";
	}
	
	public Rectangle(double x,double y)
	{
    
    
		a=x;
		b=y;
		adjustName();
	}
	
	public Rectangle(double x)
	{
    
    
		a=x;
		b=x;
		adjustName();
	}
	
	public void Rectangle(double x,double y,String n)
	{
    
    
		a=x;
		b=y;
		name=n;
	}
	
	public boolean isRectangle()
	{
    
    
		if(a>0 && b>0)
		{
    
    
			return true;
		}
		return false;
	}
	
	public void adjustName()
	{
    
    
		if(isRectangle())
		{
    
    
			if(a==b)
			{
    
    
				name="正方形";
			}
			else
			{
    
    
				name="长方形";
			}
		}
		else
		{
    
    
			name="非长方形";
		}
	}
	
	public double getPerimeter()
	{
    
    
		return (a+b)*2;
	}
	
	public double getArea()
	{
    
    
		return a*b;
	}
	
	@Override
	public String toString()
	{
    
    
		String msg=name+":边长a="+a+",边长b="+b;
		if(isRectangle())
		{
    
    
			msg=msg+",周长"+getPerimeter()+",面积"+getArea();
		}
		return msg;
	}
	
	public static void main(String[] args)
	{
    
    
		Rectangle r[]=new Rectangle[5];
		r[0]=new Rectangle();
		r[1]=new Rectangle(3);
		r[2]=new Rectangle(-2);
		r[3]=new Rectangle(5,4);
		r[4]=new Rectangle(-1,2);
		int i;
		for(i=0;i<r.length;++i)
		{
    
    
			System.out.println(r[i]);
		}
		Rectangle r1=new Rectangle();
		r1.Rectangle(10,20,"长方形");
		System.out.println(r1);
	}
	
}

operation result

Insert picture description here

Four, this keyword

The Java keyword this can only be used in the method body.

When an object is created, the Java Virtual Machine (JVM) assigns a pointer to the object—this

In a class, this can be used in non-static methods, and this must not be used in static methods and static code blocks.
This is because this can only be associated with a specific object, not a class. Different objects of the same class have different this.

Application situation

  1. Use this to call another constructor-this (parameter list). This is only used in the constructor of the class and cannot be used in other places.
  2. When a method parameter or a local variable in a method has the same name as a member variable, the member variable will be shielded, and the member variable can be accessed through this.
  3. In a method, when you need to refer to the current object of the class to which the method belongs, you can use this directly.

this exercise-triangle class

package try_a_package;

public class Triangle1 {
    
    
	private double a,b,c;
	String name="三角形";
	
	public Triangle1()
	{
    
    
		this(10);
	}
	
	public Triangle1(double x)
	{
    
    
		this(x,x,x);
	}
	
	public Triangle1(double x,double y)
	{
    
    
		this(x,x,y);
	}
	
	public Triangle1(double a,double b,double c)
	{
    
    
		this.a=a;
		this.b=b;
		this.c=c;
		adjustName();
	}
	
	public void adjustName()
	{
    
    
		if(this.isTriangle())
		{
    
    
			double  t;
			if(a>b)
			{
    
    
				t=a;
				a=b;
				b=t;
			}
			if(a>c)
			{
    
    
				t=a;
				a=c;
				c=t;
			}
			if(b>c)
			{
    
    
				t=b;
				b=c;
				c=t;
			}
			name="三角形";
			if(c*c==a*a+b*b)
			{
    
    
				name="直角"+name;
			}
			else if(a==b&&b==c)
			{
    
    
				name="等边"+name;
			}
			else if(a==b||b==c)
			{
    
    
				name="等腰"+name;
			}
		}
		else
		{
    
    
			name="非三角形";
		}
	}
	
	public boolean isTriangle()
	{
    
    
		if(a+b>c && a+c>b && b+c>a)
		{
    
    
			return true;
		}
		return false;
	}
	
	public double    getPerimeter()
	{
    
    
		return a+b+c;
	}
	
	public double getArea()
	{
    
    
		double p,s;
		p=(a+b+c)/2;
		s=Math.sqrt(p*(p-a)*(p-b)*(p-c));
		return s;
	}
	
	@Override
	public String toString()
	{
    
    
		String msg=name+"\t"+a+"\t"+b+"\t"+c;
		if(isTriangle())
		{
    
    
			msg=msg+"\t"+this.getPerimeter()+"\t"+this.getArea();
		}
		return msg;
	}
	
	public static void printHeader()
	{
    
    
		System.out.println("图形名称\t\t边长a\t边长b\t边长c\t周长\t面积");
	}
	
	public static void main(String[] args)
	{
    
    
		Triangle1 t1,t2,t3,t4,t5;
		t1=new Triangle1();
		Triangle1.printHeader();
		System.out.println(t1);
		t2=new Triangle1(6);
		t3=new Triangle1(3,4);
		t4=new Triangle1(3,4,5);
		t5=new Triangle1(3,4,7);
		
		System.out.println(t2);
		System.out.println(t3);
		System.out.println(t4);
		System.out.println(t5);
	}
}

operation result

Insert picture description here

Five, the initialization of object member properties

When initializing an object, you can use a non-static initialization block to initialize the object.
The curly braces {} of the initialization block cannot be omitted.

static modifier

Static modified member variables-static variables, static attributes, class member variables.

Class member methods cannot access non-class member variables.

For class member variables, use static initialization block to initialize.

Note: In the static initialization block, non-class member variables cannot be initialized.
But in the non-static initialization block, you can use class member variables and class member methods.

Initialization exercise-Triangle2

package try_a_package;

public class Triangle2 {
    
    
	// 第一步,初始化
	private static int id=1;
	double a=1,b=1,c=1;
	String name="三角形";
	// 第二步,静态初始化块,仅执行一次
	static
	{
    
    
		System.out.println("静态初始化(只执行一次:id——"+id);
		id=100;
		f1();
	}
	// 第三步,执行动态初始化块
	{
    
    
		System.out.println("动态初始化:id——"+id);
		System.out.println("a="+a+",b="+b+",c="+c);
		id=id+1;
		a=10;
		b=10;
		c=10;
		f2();
	}
	
	public Triangle2()
	{
    
    
		System.out.println("构造方法:id——"+id);
		System.out.println("a="+a+",b="+b+",c="+c);
		a=100;
		b=100;
		c=100;
	}
	
	public static void f1()
	{
    
    
		System.out.println("我是类成员方法f1()!");
	}
	
	public static void f2()
	{
    
    
		System.out.println("我是类成员方法f2()!");
	}
	
	@Override
	public String toString()
	{
    
    
		return "Triangle{a="+a+",b="+b+",c="+c+",name="+name+"}";
	}
	
	public static void main(String[] args)
	{
    
    
		Triangle2 t1=new Triangle2();
		System.out.println(t1+"\n");
		Triangle2 t2=new Triangle2();
		System.out.println(t2);
	}
}

operation result

Insert picture description here

Variable scope

The scope of variables is relatively simple, basically similar to C++.

Note: The scope of the exception handling parameter variable is all exception handling blocks after catch.

Guess you like

Origin blog.csdn.net/qq_41563270/article/details/108764363