Java study notes 1-classes and objects

Introduction to Java

Java is the general term for the Java object-oriented programming language and Java platform launched by Sun Microsystems in May 1995. Developed by James Gosling and colleagues, and officially launched in 1995.

Java is divided into three systems:

JavaSE (J2SE) (Java2 Platform Standard Edition, Java Platform Standard Edition)
JavaEE (J2EE) (Java 2 Platform, Enterprise Edition, Java Platform Enterprise Edition)
JavaME (J2ME) (Java 2 Platform Micro Edition, Java Platform Micro Edition).
In June 2005, the JavaOne conference was held, and SUN released Java SE 6. At this time, various versions of Java have been renamed to cancel the number "2": J2EE is renamed to Java EE, J2SE is renamed to Java SE, and J2ME is renamed to Java ME.

Java's object-oriented features

The Java language provides object-oriented features such as classes, interfaces, and inheritance. For simplicity, only single inheritance between classes is supported, but multiple inheritance between interfaces is supported, and the implementation mechanism between classes and interfaces is supported (keyword is implements). The Java language fully supports dynamic binding, while the C ++ language only uses dynamic binding for virtual functions.

When we design a class, we must know that the object is shaped by the model of the class.

Objects are known things—instance variables—
the actions that objects will perform—methods

Two uses of main ():

  • Test the real class
  • Start the Java program

java scavenging function

When an object is created, it will be stored in the heap. Java will allocate memory space according to the size of the object. When an object is considered by the JVM to be unused, it is recycled.

Guess the game

public class GuessGame
{
	Player p1;
	Player p2;
	public void startgame(){
      p1=new Player();
	  p2=new Player();
	  int gp1=0;
	  int gp2=0;
	  boolean p1right=false;
	  boolean p2right=false;
	  int n=(int) (Math.random()*10);
	  System.out.println("我准备了一个0~9的数字");
	  while(true){
		  p1.guess();
		  p2.guess();
		  gp1=p1.num;
		  System.out.println("p1猜"+gp1);
		  gp2=p2.num;
		  System.out.println("p2猜"+gp2);
		  if(gp1==n)
		  {p1right=true;}
		  if(gp2==n)
		  {p2right=true;}
		  if(p1right==true||p2right==true)
		  {
			  System.out.println("有人猜对了");
              System.out.println("p1"+p1right);
			  System.out.println("p2"+p2right);
			  break;
		  }
		  else{
		      System.out.println("接着猜");
		  }
	}
	}
}
public class Player
{
	int num=0;
	public void guess(){
	   num=(int) (Math.random()*10);
	   System.out.println("我猜"+num);
	}
}
public class  Game
{
	public static void main(String[] args) 
	{
		GuessGame g=new GuessGame();
		g.startgame();
	}
}

Insert picture description here

Published 28 original articles · won praise 2 · Views 3259

Guess you like

Origin blog.csdn.net/Maestro_T/article/details/88902603