代码改变世界

[Java in NetBeans] Lesson 04. Class / Objects

2018-12-06 03:18  Johnson_强生仔仔  阅读(333)  评论(0编辑  收藏  举报

这个课程的参考视频和图片来自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 we 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) Might also need to import the packages of others or packages in other projects. 
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);
    // StringBuilder sb = new StringBuilder(forward); is the same like last two lines. 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;