1. 程式人生 > >[Java in NetBeans] Lesson 04. Class / Objects

[Java in NetBeans] Lesson 04. Class / Objects

這個課程的參考視訊和圖片來自youtube

    主要學到的知識點有:

  • Class: Blueprint for an object. (e.g. dog is a class)
  • Object: custom variables contain state an behavior. (e.g. a two-year old husky is an object of a class dog, it's age is 2, and behavior is bark. then it will have methods like "bark()", "eat(food)")

Define an object:

 

 

  • If want to use the classes defined in Java, like Scanner, Random. Then need to use import method to import the class libraries. ("Ctrl + Shifl + i "will automatic import/unimport the packages can be refered)
import java.util.Random;

 

  • Show javadoc will help you to understand how to use the class or method.
    This
    is an example of the Random class we just import above. 
// Generator a random number between 0 - 9

Random generator = new Random();
int i = generator.nextInt(10);

 

  • Reverse a string use StringBuilder
    String forward = "This is a test";
    StringBuilder sb = new
    StringBuilder(); sb.append(forward); String reverseString = sb.reverse().toString(); System.out.println(reverseString); // System output result will be: // !tset a si sihT

     

  • Math.PI will present pi which is 3.14.... in math. ("Tab" is also our friend to see methods that we can use, so press "Tab" after Math.)
double circ = 2 * Math.PI * radius;