標題:
static 關鍵字 (一)
[打印本頁]
作者:
tonyh
時間:
2020-11-6 20:19
標題:
static 關鍵字 (一)
成員 (資料成員或方法成員) 在宣告時若使用關鍵字 static 修飾,則該成員變成屬於類別 (class) 擁有而非物件 (object) 擁有,因此我們若要在其他地方使用該成員時,不用建立實體物件,只需透過類別即可使用。譬如:Math.PI 即為靜態的資料成員,而Math.pow() 即為靜態的方法成員。
public class Ch62
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,3.8);
Dog d2=new Dog("球球",1,2.5);
d1.showProfile();
d2.showProfile();
Cat c1=new Cat("咪咪",3,3.2);
c1.showProfile();
System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓.");
}
}
class Dog
{
static int sum=0;
String name;
int age;
double w;
Dog(String n, int a, double w)
{
sum++;
name=n;
age=a;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
class Cat
{
static int sum=0;
String name;
int age;
double w;
Cat(String n, int a, double w)
{
sum++;
name=n;
age=a;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
複製代碼
作者:
陳宥穎
時間:
2020-11-6 20:47
public class Ch30 {
public static void main(String[] args) {
Dog aa=new Dog("和漢",3,1.5,"棕");
aa.showP();
aa.makesound(5);
Dog ab=new Dog("球球",5,2.3,"白");
ab.showP();
ab.makesound(3);
Cat b=new Cat("喵貓",1,0.8,"白");
b.showP();
b.makesound(5);
System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓");
}
}
class Dog
{
static int sum=0;
int age;
double sou,g;
String name,col;
Dog(String a,int x,double y,String q)
{
sum++;
name =a;
col=q;
age=x;
g=y;
}
void showP()
{
System.out.println(name+"今年"+age+"歲,體重"+g+"公斤,毛色為"+col+"色");
}
void makesound(int n)
{
for(int i=1; i<=n; i++)
System.out.print("旺~");
System.out.println();
}
}
class Cat
{
static int sum=0;
int age;
double sou,g;
String name,col;
Cat(String a,int x,double y,String q)
{
sum++;
name =a;
col=q;
age=x;
g=y;
}
void showP()
{
System.out.println(name+"今年"+age+"歲,體重"+g+"公斤,毛色為"+col+"色");
}
void makesound(int n)
{
for(int i=1; i<=n; i++)
System.out.print("喵~");
System.out.println();
}
}
複製代碼
作者:
劉愷鈞
時間:
2020-11-6 20:50
本帖最後由 劉愷鈞 於 2020-11-13 18:47 編輯
public class Ch01 {
public static void main(String[] args) {
Dog Dog1 =new Dog("憨憨",2,1.3,"紅棕色");
Dog Dog2=new Dog("球球",1,1.2,"白色");
Cat Cat1=new Cat("咪咪",3,1.5,"銀灰色");
Dog1.showProfile();
Dog1.makeSound(2);
System.out.println();
Dog2.showProfile();
Dog2.makeSound(3);
System.out.println();
Cat1.catshowProfile();
Cat1.catmakeSound(5);
System.out.println();
System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓。");
}
}
class Dog
{
static int sum=0;
String name;
int age;
double weight;
String fur;
Dog(String name,int age,double weight,String fur)
{
sum++;
this.name=name;
this.age=age;
this.weight=weight;
this.fur=fur;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+fur+"。");
}
void makeSound(int n)
{
for(int a=1;a<=n;a++)
{
System.out.print("汪~");
}
}
}
class Cat
{
static int sum=0;
String name;
int age;
double weight;
String fur;
Cat(String name,int age,double weight,String fur)
{
sum++;
this.name=name;
this.age=age;
this.weight=weight;
this.fur=fur;
}
void catshowProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+fur+"。");
}
void catmakeSound(int n)
{
for(int a=1;a<=n;a++)
{
System.out.print("喵~");
}
}
}
複製代碼
作者:
黃宥華
時間:
2020-11-6 20:50
public class Ch50 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨", 2, 3.8);
Dog d2=new Dog("球球", 1, 2.5);
Cat c1=new Cat("喵喵", 5, 3.2);
d1.showProfile();
d2.showProfile();
c1.showProfile();
System.out.println("有"+Dog.sum+"隻狗"+Cat.sum+"隻貓");
}
}
class Dog
{
static int sum;
String name;
int x;
double y;
Dog(String n,int x,double d)
{
sum++;
name=n;
this.x=x;
this.y=d;
}
void showProfile()
{
System.out.println(name+"今年"+x+"歲,體重"+y+"公斤");
}
}
class Cat
{
static int sum;
String name;
int x;
double y;
Cat(String n,int x,double d)
{
name=n;
sum++;
this.x=x;
this.y=d;
}
void showProfile()
{
System.out.println(name+"今年"+x+"歲,體重"+y+"公斤");
}
}
複製代碼
作者:
董宸佑
時間:
2020-11-6 20:53
本帖最後由 董宸佑 於 2020-11-6 21:03 編輯
public class Ch01 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨",2,1.3f);
Dog d2=new Dog("球球",1,1.2f);
d1.showProfile();
d2.showProfile();
Cat c1=new Cat("咪咪",3,1.5f);
c1.showProfile();
System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓.");
}
}
class Dog
{
static int sum=0;
String name;
int age;
float w;
Dog(String n,int a,float w)
{
sum++;
name=n;
age=a;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
class Cat
{
static int sum=0;
String name;
int age;
float w;
Cat(String n,int a,float w)
{
sum++;
name=n;
age=a;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
複製代碼
作者:
李宇澤
時間:
2020-11-6 20:54
public class Ch05
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,3.8);
Dog d2=new Dog("球球",1,2.5);
d1.showProfile();
d2.showProfile();
Cat c1=new Cat("咪咪",3,3.2);
c1.showProfile();
System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓.");
}
}
class Dog
{
static int sum=0;
String name;
int age;
double w;
Dog(String n, int a, double w)
{
sum++;
name=n;
age=a;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
class Cat
{
static int sum=0;
String name;
int age;
double w;
Cat(String n, int a, double w)
{
sum++;
name=n;
age=a;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
複製代碼
作者:
黃柏叡
時間:
2020-11-6 21:00
public class Ch01
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,3.8f);
Dog d2=new Dog("球球",1,2.5f);
d1.showProfile();
d2.showProfile();
Cat c1=new Cat("咪咪",3,3.2);
c1.showProfile();
System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓.");
}
}
class Dog
{
static int sum=0;
String name;
int age;
float w;
Dog(String n, int a, float w)
{
sum++;
name=n;
age=a;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
class Cat
{
static int sum=0;
String name;
int age;
double w;
Cat(String n, int a, double w)
{
sum++;
name=n;
age=a;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
複製代碼
作者:
孫嘉駿
時間:
2020-11-6 21:00
public class Ch01
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,3.8);
Dog d2=new Dog("球球",1,2.5);
d1.showProfile();
d2.showProfile();
Cat c1=new Cat("咪咪",3,3.2);
c1.showProfile();
System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓.");
}
}
class Dog
{
static int sum=0;
String name;
int age;
double w;
Dog(String n, int a, double w)
{
sum++;
name=n;
age=a;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
class Cat
{
static int sum=0;
String name;
int age;
double w;
Cat(String n, int a, double w)
{
sum++;
name=n;
age=a;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
複製代碼
作者:
林政瑜
時間:
2020-11-6 21:00
public class Ch62
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,3.8);
Dog d2=new Dog("球球",1,2.5);
d1.showProfile();
d2.showProfile();
Cat c1=new Cat("咪咪",3,3.2);
c1.showProfile();
System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓.");
}
}
class Dog
{
static int sum=0;
String name;
int age;
double w;
Dog(String n, int a, double w)
{
sum++;
name=n;
age=a;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
class Cat
{
static int sum=0;
String name;
int age;
double w;
Cat(String n, int a, double w)
{
sum++;
name=n;
age=a;
this.w=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
}
}
複製代碼
作者:
夏子涵
時間:
2020-11-13 18:46
public class Ch01 {
public static void main(String[] args) {
Dog d1=new Dog("憨憨", 2, 1.3f, "紅棕色");
Dog d2=new Dog("球球", 1, 1.2f, "白色");
Cat c1=new Cat("咪咪", 3, 1.5f, "銀灰色");
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
c1.showProfile();
c1.makeSound(5);
System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓.");
}
}
class Dog
{
static int sum=0;
String name;
int age;
float w;
String color;
Dog(String n,int a,float w,String c)
{
sum++;
name=n;
age=a;
this.w=w;
color=c;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
}
void makeSound(int n)
{
for(int i=1; i<=n; i++)
System.out.print("汪~");
System.out.println("");
}
}
class Cat
{
static int sum=0;
String name;
int age;
float w;
String color;
Cat(String n,int a,float w,String c)
{
sum++;
name=n;
age=a;
this.w=w;
color=c;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
}
void makeSound(int n)
{
for(int i=1; i<=n; i++)
System.out.print("喵~");
System.out.println("");
}
}
複製代碼
作者:
沈子晏
時間:
2020-11-13 18:53
public class Ch01 {
public static void main(String[] args) {
Dog number1=new Dog("憨憨",2,1.3f);
Dog number2=new Dog("球球",1,1.2f);
Cat n3= new Cat("咪咪",3,1.5f);
number1.showProfile();
number2.showProfile();
n3.showProfile();
System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓.");
}
}
class Dog
{
static int sum=0;
String name;
int age;
float weight;
Dog (String n,int a,float w)
{
sum++;
name=n;
age=a;
weight=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤");
}
}
class Cat
{
static int sum=0;
String name;
int age;
float weight;
Cat (String n,int a,float w)
{
sum++;
name=n;
age=a;
weight=w;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤");
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2