[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;

猜你喜欢

转载自www.cnblogs.com/Johnsonxiong/p/10074699.html
今日推荐