1. 程式人生 > 其它 >Java簡單邏輯運算以及static與new物件實驗

Java簡單邏輯運算以及static與new物件實驗

public class Helloworld {
static int e =6;
public static void main(String[] args) {
boolean a =true;
boolean b =false;
System.out.println("a&&b:"+(a&&b));//a與b a&&b:false
System.out.println("a||b:"+(a||b));//a並b a||b:true
System.out.println("!(a&&b):"+!(a&&b));//a與b取非 !(a&&b):true
int c =5;
boolean d =(c<4)&&(++c>6);
System.out.println(c); // 5
System.out.println(d); // false
/* =========================================================
=========================================================*/

Helloworld a1=new Helloworld(); //建立類a1
Helloworld b1=new Helloworld(); //建立類b1
Helloworld c1=new Helloworld(); //建立類c1
System.out.println(a1); //輸出"Helloworld@4554617c"(儲存地址)
System.out.println(b1); //輸出"Helloworld@74a14482"(儲存地址)
a1.e=7; //在al中呼叫e,並定義其為7
System.out.println(a1.e); //輸出結果為7
System.out.println(b1.e); //輸出結果為7
System.out.println(c1.e); //輸出結果為7

/*=============================================================================下方程式碼為實驗new+物件與static的作用而寫的實驗性程式碼,無實際意義 ==============================================================================*/

/* String[] a3={"abcd"};
a1.main(a3);無意義程式碼*/
}
public void asd(String[] args) {
ABCD jkh= new ABCD();
String[] a3={"abcd"};
jkh.al (a3);
ABCD.al (a3);
jkh.sdf (a3);
ABCD.sdf(a3); //(報錯)
}
}
class ABCD{
public static void al(String[] args) {
}
public void sdf(String[] args) {
}
}
該實驗得到效果為:
static在一個專案中定義的方法(主要main,該實驗中有asd、al、sdf)可以被物件以及類呼叫,如果沒有static則可以被類呼叫,卻不能被物件呼叫。