Basic knowledge
Java is a pure object-oriented language developed by the C + + language , and its basic elements include simple data types and composite data types (i.e., classes). Class is an abstract description of objective things, it has four object-oriented characteristics, namely: encapsulation, inheritance, polymorphism and communication correlation.
classes are made up of properties and methods , and classes, properties, and methods are limited by the use of modifiers. Common modifiers include: public, protected, private, static, final, abstract, and so on. In addition to classes, there are abstract classes and interfaces in the Java language, where abstract classes are classes that use the abstract modifier, which contains abstractions (that is, only the declaration of methods, no method body, and the abstract method needs to be overridden by its subclasses), and the key of the interface is interface. It contains only member variables and abstract methods that are decorated with finall. The relationship between classes is inheritance (extends), the relationship between interfaces is also inheritance (extends), and the relationship between classes and interfaces is implementation (implements).
There are three types of methods in a class, namely, the construction method, the common method, and the main method. The constructor method is the way the object is initialized, and there are generally multiple construction methods that construct different objects based on the parameters that are brought in. The Main method is the entry for the program, in the fixed format: public static void Main (String [] args). The methods in the class are polymorphic, namely: overloading and overwriting. Where overloads are methods with the same name in the same class, with different types and numbers of arguments, overrides are methods with the same name between the parent and child classes that have an inheritance relationship, and the type and number of parameters and the return values are the same.
When you use a class to declare an object, you are creating a different entity, that is, instantiating it. And you need to use the new operator to request memory space. When the object is no longer needed, Java has three garbage collection methods: The JVM recycles automatically, calls the System.GC () method, and defines the Finalize () destructor method.
The Java language Identifier naming convention is as follows:
The identifier for the Java language consists of a letter, a number, an underscore (_), a dollar sign ($), and the first character cannot use a number;
Cannot use keywords as identifiers
Identifiers are usually made up of a number of meaningful English words with a length of 30 characters or less
In order to ensure a good programming style, clear identification of various symbols, the Java language has the following programming specifications:
Class name and Interface name: The first letter of the word uppercase, the remaining letters lowercase, such as samdoc;
Method name and variable name: The first word is lowercase, the remaining words are capitalized, the remaining letters are lowercase, such as botheyesofdoll;
Package Name: All lowercase letters, such as Com.abc.dollapp;
Constant name: In uppercase, the words are separated by an underscore "_", such as Default_color_dol.
All the keywords are lowercase.
Code instance
Examples of rectangular and cuboid classes, imitation of round and column classes
1 classrectangle{2 Doublewidth;3 Doublelength;4 Publicrectangle () {5width = 10;6Length = 10;7 }8 PublicRectangleintWidthintlength) {9 This. width =width;Ten This. length =length; One } A PublicRectangleDoubleWidthDoublelength) { - This. width =width; - This. length =length; the } - Public Doublegetcircle () { - return(Width +length); - } + Public DoubleGetarea () { - returnWidth *length; + } } A at classCubeextendsrectangle{ - Doubleheight; - PublicCube () { -width = 10; -Length = 10; -Height = 10; in } - PublicCubeDoubleWidth1,DoubleLength1,Doubleheight) { towidth =width1; +Length =length1; - This. Height =height; the } * Public DoubleGetvol () { $ returnWIDHT * Length *height;Panax Notoginseng } - Public DoubleGetarea () { the return(Width * length + width * height + length *height); + } A } the classexample{ + Public Static voidmain (String [] args) { -Rectangle r =NewRectangle (20, 20); $Cube C =NewCube (11, 22, 33); $System.out.println ("area of the rectangle" +R.getarea ()); -System.out.println ("Volume of Box" +C.getvol ()); -System.out.println ("Area of box" +C.getarea ()); the} }
View Code
Design According to the date of the month output calendar of the program, compared to the following 2 dates program
1 Public classdate{//There is only one main method for this class, which is equivalent to a C language program2 Public Static voidmain (String [] args) {3 intYear = 2013;4 intmonth = 3;5 intDay = 0;6 BooleanIsleap =false;7 8 if(Year% 400 = = 0) | | (Year% = = 0) && (year% 4 = = 0))){9Isleap =true;Ten}Else { Oneisleap=false; A } - Switch(month) { - Case1: Case3: Case5: Case7: Case8: Case10: Case12: theDay = 31; - Break; - Case2: -Day =if(isleap)? 29:28; + Break; - default: +Day = 30; A Break; at } - for(inti=0; i<day; i++){ - if(i% 7 = = 0) System.out.println (); -System.out.print ((i+1) + "Day" + "/t"); -} } }
View Code
In accordance with the class of students for example, writing human, classmate class
1 classhuman{2 String name;3 Int age;4 Char gender;5 PublicHuman (String name,intAgeChargender) {6THIS.name =name;7This.age =Age ;8This.gender =gender;9 }Ten Public voidsetName (String name) { OneTHIS.name =name; A } - Public voidSetage (intAge ) { -This.age =Age ; the } - Public voidSetName (Chargender) { -This.gender =gender; - } + PublicString GetName () { - returnname; + } A Public intGetage () { at returnAge ; - } - Public CharGetgender () { - returngender; - } - Public voidShowMessage () { inSystem.out.println ("Name:" +name+ ", Age:" +age+ ", gender" +gender); - } } to //Student Class + classClassmateextendshuman{ - String School; the PublicClassmate (String name1,intAge1,CharGender1, String School) { *Name =name1; $Age =Age1;Panax NotoginsengGender =Gender1; - This. School =School; the ... +}
View Code
Java Learning---class design for Java