zoukankan      html  css  js  c++  java
  • Java初学几个程序背诵

    1.Hello world

    class first
    {
        public static void main(String[] args){
            System.out.println("Hello world");
        }
    }

    2.数组 -> for循环

     1 class first
     2 {
     3     public static void main(String[] args){
     4         int[] array;
     5         array = new int[5];
     6         for(int i=0;i<5;i++){
     7             array[i]=i;
     8         }
     9         for(int i=4;i>=0;i--){
    10             System.out.println(array[i]);
    11         }
    12     }
    13 }

    3.简单的类:class

     1 class first
     2 {
     3     public static void main(String[] args){
     4         Human Person = new Human() ;
     5         Person.age();
     6         System.out.println(Person.height);
     7     }
     8 }
     9 class Human
    10 {
    11     void age(){
    12         System.out.println("I am 12");
    13     }
    14     int height = 10;
    15 }

     4.构造函数

     1 class first
     2 {
     3     public static void main(String[] args){
     4         Human Person = new Human(180) ;
     5         Person.age();
     6         System.out.println(Person.height);
     7     }
     8 }
     9 class Human
    10 {
    11     public Human(int h){
    12         this.height =h;
    13         System.out.println("I am SmarTop");
    14     }
    15     void age(){
    16         System.out.println("I am 12");
    17     }
    18     int height = 10;
    19 }

    5.重载

     1 class first
     2 {
     3     public static void main(String[] args){
     4         Human Person = new Human(180,"SmarTop") ;
     5         Person.age();
     6         System.out.println(Person.height);
     7     }
     8 }
     9 class Human
    10 {
    11     public Human(int h){
    12         this.height =h;
    13         System.out.println("I am SmarTop");
    14     }
    15     Human(int h , String name){
    16         this.height = h;
    17         this.name = name;
    18         System.out.println("Name:"+name+"
    Height:"+h);
    19     }
    20     void age(){
    21         System.out.println("I am 2");
    22     }
    23     int height = 10;
    24     String name;
    25 }

    6.封装和接口

    class two
    {
        public static void main(String[] args){
            Human aPerson = new Human(160);
            System.out.println(aPerson.getHeight());
            aPerson.growHeight(170);
            System.out.println(aPerson.getHeight());
            aPerson.replaceBreath(100);
        }
    }
    class Human
    {
        public Human(int h){
            this.height = h;
            System.out.println("I am born");
        }
        public int getHeight(){
            return this.height;
        }
        public void growHeight(int h){
            this.height = this.height + h;
        }
        private void breath(){
            System.out.println("I am nicia");
        }
        public void replaceBreath(int rep){
            int i;
            for(i=0;i<rep;i++){
                this.breath();
            }
        }
        private int height;
    }
  • 相关阅读:
    Javascript FP-ramdajs
    微信小程序开发
    SPA for HTML5
    One Liners to Impress Your Friends
    Sass (Syntactically Awesome StyleSheets)
    iOS App Icon Template 5.0
    React Native Life Cycle and Communication
    Meteor framework
    RESTful Mongodb
    Server-sent Events
  • 原文地址:https://www.cnblogs.com/subtract/p/4223998.html
Copyright © 2011-2022 走看看