Object-oriented (java cainiao's class notes) and java class notes
Class: Put the same things together into attributes and actions; describe the same characteristics of one or more groups of things = "class attributes and actions are called members: // declare the attribute information of the class public class ** {String name; int age; char sex;} // declare the action information of the class (in the attribute information) public void eat () {System. out. println ("eating");} public void study () {System. out. println ("study");} public void playgame () {System. out. println ("playing ");} class = abstract class: only individuals in an abstract class can perform actual operations. abstract process object = Class Object process class = Class Object object-oriented class = object-oriented class; generate an object (in the main class) student student1 = new student (); assign a value to the object (in the main class) student. name = "***"; constructor: when instantiating the constructor, its name must be the same as the class name, and there is no return value type; static class-level property static int ***; the method modified by static is called static method, which can only call static properties; the anonymous object can only call this once. it indicates that the java object of the current class can also be added, deleted, modified, and queried. The Calling method can be called for loop. The flage switch; public private only access the protected subclass and the parent class are both external prohibited access extends inheritance the process of separating the parts of the two classes is called generalization. the class is called subclass to inherit the parent class Elements Using extends; the sub-classes from the parent class to the Child class are called special; all classes inherit the Object class by default; one class can only inherit one parent class; they can inherit indirectly and directly; and indirectly inherit the parent class of the parent class; method duplication is equivalent to extending the members of the parent class. If the attributes are the same, the Child class is displayed and the parent class is overwritten. unless the parent class is declared to be called, the parent class constructor is called first, then the sub-class constructor is called. super () calls the parent class constructor, which must be written in the first line; written in the sub-class constructor; instanceof (used for strong conversion) determines whether a class is used; rewrite Rules: (subclass) two small ones one big one this (); call the parameter-free constructor In the parameter Constructor (must be written in the first line) to convert other types to int; if there is a numeric formatting exception that cannot be converted, int ** = Integer. parseInt (***) ArrayIndexOutofBoundsExceptionNumberFarmatException number formatting Exception catch: try {} catch (Exception e) {} String * = I + ""; the variable defined by final is converted to a constant in String mode. The following is a supermarket management system:
Import java. util. *; // package Adjustment
Public class Main {// create a Main class
Static pipeline SC = new pipeline (System. in );
Static int I = 0;
Static Chaoshi [] shop = new Chaoshi [100];
Public static void main (String [] args) {// main Entry
Menu (); // call the menu Method
}
Public static void menu () {// defines a menu Method
System. out. println ("=========================== ");
System. out. println ("" welcome to the supermarket inventory statistics System 《");
System. out. println ("=" Enter the corresponding number to operate 《");
System. out. println ("========" 1, purchase storage 《");
System. out. println ("=========" 2, modify the information 《");
System. out. println ("========" 3, query the inventory 《");
System. out. println ("=========4, exit the operating System 《");
System. out. println ("=========================== ");
Int a = SC. nextInt (); // user input
Switch (){
Case 1: adds (); break;
Case 2: update (); break;
Case 3: select (); break;
Case 4: exit (); break;
Default: System. out. println ("==== incorrect input. Please enter another === ");
Menu ();
Break;
}
}
Public static void adds () {// define an inventory method
System. out. println ("========== enter the goods name ========= ");
String sname = SC. next ();
System. out. println ("========== enter the cargo type ========= ");
String sclass = SC. next ();
System. out. println ("=========== enter the goods price ========= ");
String sprice = SC. next ();
System. out. println ("=========== enter the number of goods ========= ");
String snum = SC. next ();
Chaoshi shop1 = new Chaoshi (sname, sclass, sprice, snum );
Shop [I] = shop1;
I ++;
System. out. println ("input successful, continue input (1), return menu (2 )");
Int flage = SC. nextInt ();
If (flage = 1 ){
Adds ();
} Else menu ();
}
Public static void update () {// define a modification method
System. out. println ("Enter the product name and modify it for you ");
String B = SC. next ();
For (I = 0; I <100; I ++ ){
If (B. equals (shop [I]. sname )){
System. out. println ("=========================== the query result is as follows ======================== ==== ");
Shop [I]. say ();
System. out. println ("========= enter the goods name after modification ========= ");
String sname = SC. next ();
System. out. println ("========= enter the goods type after modification ========= ");
String sclass = SC. next ();
System. out. println ("========== enter the goods price after modification ======== ");
String sprice = SC. next ();
System. out. println ("========== enter the goods quantity after modification ========== ");
String snum = SC. next ();
Chaoshi shop1 = new Chaoshi (sname, sclass, sprice, snum );
Shop [I] = shop1;
System. out. println ("continue to modify (1), return to menu (2 )");
Int flage = SC. nextInt ();
If (flage = 1 ){
Update ();
} Else menu ();
}
}
}
Public static void select () {// define a query method
System. out. println ("specified query (1), all queries (2 )");
Int a = SC. nextInt ();
If (a = 1 ){
System. out. println ("Enter the product name for your query ");
String B = SC. next ();
For (I = 0; I <100; I ++ ){
If (B. equals (shop [I]. sname )){
System. out. println ("=========================== the query result is as follows ======================== ==== ");
Shop [I]. say ();
System. out. println ("Continue query (1), return to menu (2 )");
Int flage = SC. nextInt ();
If (flage = 1 ){
Select ();
} Else menu ();
}
}
} Else {
For (Chaoshi s: shop ){
If (s! = Null ){
S. say ();
}
}
System. out. println ("Continue query (1), return to menu (2 )");
Int flage = SC. nextInt ();
If (flage = 1 ){
Select ();
} Else menu ();
}
}
Public static void exit () {// defines an exit Method
System. out. println ("= ");
System. exit (0 );
}
}
Public class Chaoshi {// create a supermarket class
String sname;
String sclass;
String sprice;
String snum;
Public Chaoshi (String sname, String sclass, String sprice, String snum ){
This. sname = sname;
This. sclass = sclass;
This. sprice = sprice;
This. snum = snum;
}
Public void say (){
System. out. println ("name:" + sname + "\ n type:" + sclass + "\ n price:" + sprice + "\ n quantity: "+ snum +" \ n "); // defines an output mode.
}
}