I. Understanding what classes and objects are
Everything is object.
1. Attributes--Characteristics of objects (characteristics)
2, Method--the action that the object can perform (what to do)
3, the definition of the object:
An objective, visible, or tangible entity composed of attributes and methods .
4, the class is abstracted by the object, can't see also touch
5. What are the relationships between classes and objects?
The class (which is general) is the abstraction of the object, and the object (concrete) is the materialization of the class.
String Schoolname; Center name int classnumber; Number of classrooms int labnumber; Number of rooms //define the method of Beida Jade Bird Center public Void Showcenter () { System.out.println (schoolname+ "Training center \ n" + "equipped with:" + classnumber+ "teach" +labnumber+ "machine"); }
public static void Main (String [] args) { No1 center=new No1 (); SYSTEM.OUT.PRINTLN ("* * * * Initialize member variable after"); Center.showcenter (); Center.schoolname= "Beijing Voyage"; Assigning a value to the Schoolname attribute center.classnumber=10; Assigning a value to the Classnumber attribute center.labnumber=10; Assign a value to the Labnumber property System.out.println ("Initialize member variable"); Center.showcenter (); }
Second, package--packaging
1.2 Forms of expression: properties and Methods
2, the role of packaging:
Hide internal implementation details to make it easy to operate
3, the package embodied in Java:
Class--All Java code is written in the class (method)
MyEclipse tools in the form of projects, encapsulation
Iii. Creating and Using objects
1. Create a syntax for a class
Access modifier Class class name {
Properties
public void Method name () {
Cyclic structure, condition judgment (if, switch)
Output statement
}
}
2. Create objects
Class Name Object name =new class name ();
Scanner input=new Scanner (system.in);
How do I invoke properties and methods?
Member variable (property name), by: Object name. property name;
Method, through: Object name. Method Name ();
Using object-oriented to make a simple computer
Double A; Double b; Char Yun; public void Suan () { switch (Yun) {case ' + ': System.out.println (A + "+" +b+ "=" + (a+b)); break; Case '-': System.out.println (A + "-" +b+ "=" + (a) "); break; Case ' * ': System.out.println (A + "*" +b+ "=" + (a*b)); break; Case '/': if (b!=0) { System.out.println (A + "/" +b+ "=" + (A/b)); } else{ System.out.println ("The divisor cannot be 0! "); } break; } }
public static void Main (String [] args) { No7 s=new No7 (); Scanner input=new Scanner (system.in); System.out.print ("Please enter the first number"); S.a=input.nextdouble (); System.out.print ("Please enter a second number"); S.b=input.nextdouble (); System.out.println ("Please enter operator (+ 、-、 *,/)"); S.yun=input.next (). charAt (0); S.suan (); }
Object oriented
Class--noun
Properties--Adjectives
Method--Verb
1. Communication between classes (Access): An object in a class that is new to another class, through the name of the object. The member name of the class
Members of the class: attributes (member variables), methods (member methods)
2. Advantages:
Information encapsulation and hiding,
Improve program security, reusability, maintainability
Five, commissioning
1. Set breakpoints
2, start debugging
3, press F6 progressive execution program
When you encounter the "method", debug the code in the method, press F5
Step 1: Write the Class (properties, Methods)
property is not assigned a value in this class
method to output the value of the property
Step 2: Create a new class with main (test Class)
Step 3:
Create an object for the class of step 1
Object name. property = value; (from console assignment)
The name of the object. Method name ();//The value of the property is output
/** old name */ String oldname; /** Old password */ String OLDMI; /** New name */ String name; /** Password */ String passwords; /** New Password */ String newpasswords; /** output account's Show method * /public void Show () { Scanner input=new Scanner (system.in); Boolean f=false; /** use the cycle to judge * /do{System.out.print ("Please enter Name:"); Name=input.next (); System.out.print ("Please enter password:"); Passwords=input.next (); /** */ if (name.equals (oldname) &&passwords.equals (OLDMI)) { System.out.print ("\ n Please enter a new password"); Newpasswords=input.next (); SYSTEM.OUT.PRINTLN ("Password modified successfully!") The new password is: "+newpasswords); F=true; break; } else{ System.out.println ("Password input error! You do not have permission to change the password. "); break; } } while (f==true); }
public static void Main (String [] args) { Scanner input=new Scanner (system.in); No11 d=new No11 (); D.oldname= "Admin1"; d.oldmi= "1111"; D.show (); }
Java Fundamentals 7