From scratch learn Java-9. description object, learn java-9 object

Source: Internet
Author: User

From scratch learn Java-9. description object, learn java-9 object


1. Create variables for classes or objects;
2. Use methods of objects and classes;
3. Call the method and return a value;
4. Create a constructor;
5. Pass parameters to the method;
6. Use this to reference objects;
7. Create a new object.

Program VirusLab: test the functions of the Virus class

 1 package com.jsample; 2  3 public class VirusLab { 4     public static void main(String[] args){ 5         int numViruses = Integer.parseInt(args[0]); 6         if (numViruses > 0){ 7             Virus[] virii = new Virus[numViruses]; 8             for(int i = 0; i < numViruses; i++){ 9                 virii[i] = new Virus();10             }11             System.out.println("There are " + Virus.getVirusCount()12                 + " viruses.");13         }14     }15 }
View Code

 


Input: 19000000
Output:

There are 19000000 viruses.


Virus: used to simulate a Virus attribute in a very rough way.

1 package com. jsample; 2 3 public class Virus {4 static int virusCount = 0; // class Object, counting the number of Virus class objects 5 6 private int newSeconds; // breeding speed 7 8 public Virus () {9 virusCount ++; // Method for auto-incrementing new Virus 10} 11 12 public int getNewSeconds () {// return the value of newSeconds 13 return newSeconds; 14} 15 16 public void setNewSeconds (int newValue) {// when the new value is between 60 and, set newSeconds to the provided value 17 if (newValue >=60 & newValue <= 100) 18 newSeconds = newValue; 19} 20 21 static int getVirusCount () {22 return virusCount; 23} 24}
View Code

 

Program Change: accepts a string parameter and converts it to a float variable, a Float object, and an int variable.

 1 package com.jsample; 2  3 public class Change { 4     public static void main(String[] args){ 5         System.out.println(); 6         System.out.println(args[0]); 7         float a = Float.parseFloat(args[0]); 8         System.out.println(a); 9         Float b = a;10         System.out.println(b);11         int c = (int)a;12         System.out.println(c);13     }14 }
View Code

 

Input: 190.52

Output:

190.52
190.52
190.52
190

Related Article

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.