package helloworldapp;
public class class_basics {
/** field */
private int cadence; //节奏
private int gear;
private int speed;
/** constructor */
public class_basics(int startCadence,int startSpeed, int startGear){
this.gear = startGear;
this.speed = startSpeed;
this.cadence = startCadence;
ID = ++number_class_baiscs_instantiated; // assign new id to new object and increase global counting by 1
}
/** get,set method */
public int getGear() {
return gear;
}
public void setGear(int gear){
this.gear = gear;
}
public void setCadence(int cadence) {
this.cadence = cadence;
}
public int getCadence() {
return cadence;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
/** overloading method */
public void draw(String s){}
public void draw(int i){}
/** array as parameter */
public String print(String[] mystrings){
return "";
}
/** arbitrary number of arguments */
public String print(int... myints){ // 和 int[] myints 作为参数效果一样
int a = myints[1];
int length = myints.length;
System.out.printf("%s: %d","the length of input array is",length);
return "";
}
/** field multiple in one row */
private int x,y,radius;
/** explicit constructor invocation 显式调用另外的constructor */
public class_basics(int startCadence){ //好处是可以提供默认生成/简略生成的方式
this(startCadence,0,1);
}
public class_basics(int startCadence,int startSpeed){
this(startCadence,startSpeed,1);
}
/** access level 不同attribute的权限:
* Modifier Class Package Subclass World
* public Y Y Y Y
* protected Y Y Y N
* no modifier Y Y N N
* private Y N N N
* */
/** class variable (static)*/
private int ID;
private static int number_class_baiscs_instantiated = 0;
/** class method (static)*/
public static int getNumber_class_baiscs_instantiated(){
return number_class_baiscs_instantiated;
}
/** constant */
static final double PI = 3.41592653589793;
/** initializing fields */
public static int capacity = 10;
/** static initialization blocks */
static{
int firstint = 1;
}
/** nested classes */
class innerClass{}
/** acces from main:
* class_basics cb = new class_basics();
* class_basics.innerClass ic = cb.new innerClass();
* */
static class staticNestedClass{}
/** local class: define a class in for-loop or a method */
private static void validatePhoneNumber(String num1,String num2){
final int numLen = 10;
final String regularExp = "[^0-9]";
class PhoneNum{
String formattedPhoneNumber = null;
PhoneNum(String num){
String currentNum = num.replaceAll(regularExp,"");
}
}
}
/** anonymous class: define a class in instantiation */
interface helloworld{
public void greet();
public void greetSomeone(String someone);
}
helloworld englishGreeting = new helloworld() {
String name = "idiot";
@Override
public void greet() {
greetSomeone("nice 2 meet u");
}
@Override
public void greetSomeone(String someone) {
name = someone;
System.out.print("hello"+name);
}
};
helloworld germanGreeting = new helloworld() {
@Override
public void greet() {
greetSomeone("Herr XXX");
}
@Override
public void greetSomeone(String someone) {
System.out.print("Hallo"+someone);
}
};
/** lambda expression: the anonymous method without name*/
interface IntegerMath{
int operation(int a ,int b);
}
public int doOpeartion(int a, int b, IntegerMath m){
return m.operation(a,b);
}
private void lambdatest(){
IntegerMath addition = (a,b) -> a+b;
// addition.operation(1,2);
System.out.println(doOpeartion(1,2,addition));
}
/** enum */
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
public class EnumTest{
Day day;
public EnumTest(Day day) {
this.day = day;
}
}
}
联系方式:clarence_wu12@outlook.com