Class 4 and objects

Source: Internet
Author: User

Class 4 and objects

    1. The difference between a class and an object
      1. Classes are abstract and conceptual and represent a class of things, such as humans and animals...
      2. Objects are specific and actually exist, representing a specific food
      3. A class is an object template. An object is an instance of a class. Classes are used to instantiate an object. For example, a specific person who creates a wheat by a person.
    2. How to defineClass?
    3. How to createObject? -- Two methods
      1. First
        1. Object Declaration: Class Name Object Name
        2. Object creation: Object Name = New Class Name ()
      2. Second: One-step deployment
        1. Class Name object name = New Class Name ();
    4. The first letter of the class name is capitalized;
    5. Class member method (member function) Definition

      1. Program As follows:
         
        1:/*
         
        2:* Member methods without return values by printing pyramid exercises
        3:*/
         
        4:Package lesson4;
         
        5:Public Class_ 3 printjinzita {
         
        6:/**
         
        7:* @ Param ARGs
         
        8:*/
         
        9:Public Static VoidMain (string [] ARGs ){
         
        10:// Todo auto-generated method stub
        11:Printjinzita myprintjinzita =NewPrintjinzita (9 );
         
        12:Myprintjinzita. printhalf ();
         
        13:Myprintjinzita. Print ();
         
        14:}
         
        15:}
         
        16:// Print the pyramid class
         
        17:ClassPrintjinzita {
         
        18:// Define the print Layers
         
        19:Private IntN = 0;
        20:PublicPrintjinzita (IntN ){
         
        21:This. N = N;
         
        22:}
         
        23:// Print the right half of the pyramid
         
        24:Public VoidPrinthalf (){
         
        25:For(IntI = 0; I <n; I ++ ){
         
        26:For(IntJ = 0; j <I + 1; j ++ ){
        27:System.Out. Print ("*");
         
        28:}
         
        29:System.Out. Println ();
         
        30:}
         
        31:}
         
        32:// Print the pyramid
         
        33:Public VoidPrint (){
         
        34:IntHalf_n = (This. N * 2-1)/2;
        35:For(IntI = 0; I <n; I ++ ){
         
        36:For(IntL = 0; L  
         
        37:System.Out. Print ("");
         
        38:}
         
        39:For(IntK = 0; k <I; k ++ ){
         
        40:System.Out. Print ("*");
         
        41:}
        42:For(IntJ = 0; j <I + 1; j ++ ){
         
        43:System.Out. Print ("*");
         
        44:}
         
        45:System.Out. Println ();
         
        46:}
         
        47:}
         
        48:}
      1.  
        1:The procedure is as follows:
         
        2:/*
         
        3:* Function: print the ninth multiplication table of mathematics and the member methods of the exercise class.
        4:*/
         
        5:Package lesson4;
         
        6:Public Class_ 4 chengfabiao {
         
        7:/**
         
        8:* @ Param ARGs
         
        9:*/
         
        10:Public Static VoidMain (string [] ARGs ){
         
        11:// Todo auto-generated method stub
        12:Chengfabiao =NewChengfabiao (9 );
         
        13:}
         
        14:}
         
        15:ClassChengfabiao {
         
        16:PublicChengfabiao (IntN ){
         
        17:For(IntI = 1; I <= N; I ++ ){
         
        18:For(IntJ = 1; j <= I; j ++ ){
        19:System.Out. Print (J +"*"+ I +"="+ I * j +"");
         
        20:If(I = 3 & J = 2) | (I = 4 & J = 2 )){
         
        21:System.Out. Print ("");
         
        22:}
         
        23:}
         
        24:System.Out. Println ();
         
        25:}
         
        26:}
        27:}
      1. Constructor is a special method with the following features.
        (1) The method name of the constructor must be the same as the class name.

        (2) The constructor does not return a type and cannot be defined as void. The method type is not declared before the method name.

        (3)The main function of constructor is to complete object initialization. It can pass the parameters when defining an object to the object's domain.

        (4) The constructor cannot call the constructor, but must call the constructor.

        (5) A class can define multiple constructor methods. If no constructor is defined when a class is defined, the compilation system automatically inserts a default constructor without parameters, this constructor does not execute anyCode.

        (6) The constructor can be overloaded and distinguished by the number, type, or order of parameters.

        I personally think that there is no returned rule, but it is actually an object of the class returned implicitly.

    1. Case: Implement element transpose of a Matrix

      The procedure is as follows:

       
      1:/*
       
      2:* Function: transpose a three-dimensional matrix and associate the member methods of the class.
       
      3:*/
       
      4:Package lesson4;
      5:Public Class_ 5 shuzuzhuanzhi {
       
      6:/**
       
      7:* @ Param ARGs
       
      8:*/
       
      9:Public Static VoidMain (string [] ARGs ){
       
      10:// Todo auto-generated method stub
       
      11:Shuzuzhuanzhi =NewShuzuzhuanzhi ();
       
      12:}
      13:}
       
      14:ClassShuzuzhuanzhi {
       
      15:PublicShuzuzhuanzhi (){
       
      16:IntTemp = 0;
       
      17:Int[] [] Myarr = {1, 2, 3}, {4, 5, 6}, {7, 8, 9 }};
       
      18:For(IntI = 0; I <3; I ++ ){
       
      19:For(IntJ = I + 1; j <3; j ++ ){
       
      20:Temp = myarr [I] [J];
      21:Myarr [I] [J] = myarr [J] [I];
       
      22:Myarr [J] [I] = temp;
       
      23:}
       
      24:}
       
      25:For(IntI = 0; I <3; I ++ ){
       
      26:For(IntJ = 0; j <3; j ++ ){
       
      27:System.Out. Print (myarr [I] [J] +"");
       
      28:}
      29:System.Out. Println ();
       
      30:}
       
      31:}
       
      32:}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.