Java learning notes-1

Posted by mjdamato on Sun, 27 Oct 2019 10:30:12 +0100

Article directory

I. manufacturing objects with class

  • Objects are entities that need to be created to do things for us
  • A class is a specification that creates objects based on the definition of a class
    • Object: this cat, there is a cat, specifically speaking of a specific thing. That is, objects are concrete instances of classes.
    • Cat: this kind of animal can be used to generalize the concept of every specific cat is cat. That is, a class is just a specification, a definition. It determines what all objects of this class will look like in the future.

  • Encapsulation: putting data together with operations on data
  • Those data are protected by operation and not disclosed to the public
  • The object variable is the manager of the object
  • Variables defined in a class exist in each object and are different in each object

Member variables and member functions

2.1 concept

  • Class defines the variables in an object, which are called member variables.
  • Each object has its own variables, which are separate from other objects of the same class.
  • You can write the name of the member variable directly in the function to access the member variable.
  • Function is called through object, vm.insertMoney();
    • This call temporarily establishes the relationship between insertMoney() and v (this establishment), so that the member variable inside insertMoney() refers to the member variable of v.
    • This is a special intrinsic local variable of a member function, which represents the object that called the function this time.
  • Call function: call the function of an object through the. Operator
    • Directly call other functions within the member (this will pass), while outside the member function, you need to call the member function by the name of the object.

2.2 local variables

  • Local variable: a variable defined within a function.

  • Member variables: variables defined outside the function

  • The lifetime and scope of the local variable are all within the function (once in the function, this variable will have one)

  • The lifetime of the member variables is the lifetime of the object (these member variables start to exist in the object only after the new object is released), and the scope is the member functions within the class. Java has an automatic garbage collection mechanism, so it doesn't care when it disappears in Java.

III. object initialization

3.1 initialization of member variable definition

  • Member variables can give initial values where they are defined
  • Member variables that do not give an initial value will automatically get a value of 0
    • The 0 value of the object variable means that no object is managed, and null value can also be given actively.
  • Definition initialization can call functions or even use defined member variables

3.2 constructor

  • If there is a member function with the same name as the class, it will be called automatically when each object of the class is created.
  • This function has no return value type

3.3 function overload

  • A class can have multiple constructors, as long as their parameter tables are different
  • When an object is created, different parameter values will be given, and different constructors will be called automatically.
  • Other constructors can be called through this().
  • Functions with the same name but different parameter tables of a class form an overload relationship.
  • In one constructor, you can call another constructor without parameters through this(); but it can only be put in the first sentence and can only be used once.

First week

Topic content: design a Fraction class. This class uses two variables of type int to represent the numerator and denominator respectively. The constructor of this class is:

Fraction(int a,int b)
//Construct a fraction of a/b.
  
//This class provides the following functions:
double toDouble();
//Convert score to double

Fraction plus(Fraction r);
//Add your own score and r's score to create a new Fraction object
  
 Fraction multiply(Fraction r);
  //Multiply your score and r's score to produce a new Fraction object
  
 void print(); 
//Output yourself as "numerator / denominator" to standard output with carriage return. If the score is 1 / 1, you should output 1. When the numerator is larger than the denominator, it is not necessary to propose the integral part, that is, 31 / 30 is a correct output
public class Main {
 
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		Fraction a = new Fraction(in.nextInt(), in.nextInt());
		Fraction b = new Fraction(in.nextInt(),in.nextInt());
		a.print();
		b.print();
		a.plus(b).print();
		a.multiply(b).plus(new Fraction(5,6)).print();
		a.print();
		b.print();
		in.close();
	}

}
//Main function given

//Class Fraction
class Fraction
{
	private int fenzi,fenmu;
	// Constructor, no return value type
	public Fraction(int a,int b)
	{
		this.fenzi=a;
		this.fenmu=b;
	}
	
	public double toDouble()
	{
		return fenzi*1.0/fenmu;
	}
	
	public Fraction plus(Fraction r)
	{
		Fraction m=new Fraction(0,0);
		m.fenzi=this.fenzi*r.fenmu+this.fenmu*r.fenzi;
		m.fenmu=this.fenmu*r.fenmu;
		return m;
	}
	
	public Fraction multiply(Fraction r)
	{
		Fraction m=new Fraction(0,0);
		m.fenzi = this.fenzi*r.fenzi;
		m.fenmu = this.fenmu*r.fenmu;
		return m;
		
	}
	void print() 
	{
		int r;//Remainder
		int x=fenzi,y=fenmu;
		//Find the greatest common divisor of numerator and denominator
		while(y!=0)
		{
			r=x%y;
			x=y;
			y=r;
		}
		//x is the greatest common divisor of numerator and denominator
		//Divide the numerator denominator by their common divisor to get the simplest fraction
		fenzi/=x;
		fenmu/=x;
		if(fenzi==fenmu)
		{
			System.out.println(1);
		}
		else
		{
			System.out.println(fenzi+"/"+fenmu);
		}
		return;
	}
	
}

Topics: Java