From the second week to give up the entry (object-oriented) ...... day.6 .. . . . . Objects, classes, methods;


1, object and class
2, class composition
3. declaring class
4, created by an object class
5, how to access the object's properties, methods,
concepts 6, method statements and calls pass parameters (focus)

 

 

1, with the object class
Object Class
class is an abstract template having the same characteristics and behavior, an object is a concrete realization (instance instance) class

 

 

Composition 2, like:
properties and methods, code blocks, inner class ..

 

 

3, the class declaration (custom data types)
class {} class name
of a Java file may have a plurality of Java class, but at most one public class, and the class file with the same name as the name

 

 

4, to create an object class by
new constructor ();

 

 

5, access (Access) method or property
if no explicit assignment attribute, the attribute has a default value of
use. Operator properties and methods to access the object
before accessing the property or method, the object is not null (the empty object, means unallocated space)

 


The definition of a representative of the computer classes, attributes the brand (brand), CPU, dimensions
define a test class, create two computers, which will be the property assignment, the final output

public class Diannao {
     String pp;
     String cpu;
     int cc;

}

  

 

 

6, the concept of the method of
reducing code duplication, the system maintainability may be provided

 

 

7, the syntax
access control method for modifying the return data type name (parameter list) {value
method body;
}

 


8, parameter passing method


In the form of parameters (parameters): Parameter list of methods declared in
the actual parameters (arguments): expression or variable parameters passed to the method of
actual parameter and formal parameter, the name does not matter, to match the data type, the number, order

If the parameters are the basic data types, according to by value
if the data type is a reference, in accordance with the pass-way


9, Scope Scope
variables (method thereof, parameters) declared method can only be used in the method
block
branch structure and circulation

 

 

10, variable length parameter
syntax: int ... num
variable-length argument at most one and only appear at the end of the parameter list

Call:
do not pass parameters
pass a
pass multiple, separated by commas
pass an array


 

Classroom exercises:
1, and calculates two integers 
2, it is judged whether an integer in an integer array
Boolean isExists (NUM int, int [] the nums) {
}
. 3, it is determined whether the two arrays of integers equal to
two integer array 4, calculated and the return type is an array
public int [] the Add (int [] nums1, int [] nums2) {

}
. 5, an array of inverted

6, calculates two integers arrays into ascending median array
{1,4,8,9,3,5}
{} 3,6,8,4,5,9,10

{1,3,3,4,5,6,6,9,9,10}

7, the following functions: 
7.1 to declare a product (Product) class, declare their properties (number: no, name: name, inventory: Stock)
7.2 declares a warehouse (Storage) class, property size for the product array 10
in Storage class, there is a method:
storage: parameter is the product, which product is stored in an array, wherein if no array, and the product storage space, then added to the array,
the presence of the product, which is updated inventory
library: parameter is the product name and number, first check whether the existence of the product,
if there is:
it is determined that the number of the library is less than or equal to its stock, qualifying it to reduce its inventory, or do not do the operation, suggesting that only
if there is no product, then no operation, only prompt
inventory: All products outputs warehouse

 

public class Stoarg {

	String[] x = new String[10];
	int[] y = new int[10];

	public void add(Product p) {

		for (int i = 0; i < 10; i++) {
			if (x[i] == p.name) {
				y[i]++;
				p.stock++;
			} else if (x[i] == null) {
				x[i] = p.name;
				y[i]++;
				p.stock++;
				break;
			}
		}
	}
	public void chuku(String name, int num) {

		for (int i = 0; i < 10; i++) {
			if (x[i] == name) {
				if (y[i] >= num) {
					y[i] -= num;

					break;
				} else {
					System.out.println("shuliangbuzu");
					break;
				}
			}
		}
	}

	public void xianshi() {

		for (int i = 0; i < 10; i++) {
			System.out.print(x[i] + " " + y[i] + "   ");
		}
	}
}

  

public class Product {
       int no;
       String name;
       int stock;
       
}

  

public class Producttest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
             Product bx=new Product();
             bx.name="冰箱";
             bx.no=1;
             bx.stock=0;
             
             Stoarg a=new Stoarg();
             a.xianshi();
             a.add(bx);
             System.out.print("\n");
             a.xianshi();
             
             a.chuku(bx.name, 1);
            // a.xianshi();
             
            
             Product ds=new Product();
             ds.name="电视";
             ds.no=1;
             ds.stock=0;
              
             System.out.print("\n");
             a.xianshi();
             a.add(ds);
             System.out.print("\n");
             a.xianshi();
	
             System.out.print(bx.stock);	
	}
}

  

 

 

 Conceptual thing to start the high side (hey ................)

 

                                 

 

Guess you like

Origin www.cnblogs.com/suxiao666/p/11329395.html