標題:
static 關鍵字 (一)
[打印本頁]
作者:
tonyh
時間:
2019-2-18 20:13
標題:
static 關鍵字 (一)
成員 (資料成員或方法成員) 在宣告時若使用關鍵字 static 修飾,則該成員變成屬於類別 (class) 擁有而非物件 (object) 擁有,因此我們若要在其他地方使用該成員時,不用建立實體物件,只需透過類別即可使用。譬如:Math.PI 即為靜態的資料成員,而Math.pow() 即為靜態的方法成員。
public class Ch62
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,1.3);
Dog d2=new Dog("球球",1,1.2);
d1.showProfile();
d2.showProfile();
Cat c1=new Cat("咪咪",3,1.5);
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+"公斤.");
}
}
複製代碼
作者:
李沛昂
時間:
2019-2-18 20:23
public class Pig
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,1.3);
Dog d2=new Dog("球球",1,1.2);
d1.showProfile();
d2.showProfile();
Cat c1=new Cat("咪咪",3,1.5);
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+"公斤.");
}
}
複製代碼
作者:
趙一鳴
時間:
2019-2-18 20:24
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,color;
int age;
float weight;
Dog(String n,int a,float w,String c)
{
name=n;
age=a;
weight=w;
color=c;
sum++;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲, 體重"+weight+"公斤, 毛色為"+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,color;
int age;
float weight;
Cat(String n,int a,float w,String c)
{
name=c;
age=a;
weight=w;
color=c;
sum++;
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲, 體重"+weight+"公斤, 毛色為"+color+".");
}
void makeSound(int n)
{
for(int i=1;i<=n;i++)
{
System.out.print("喵~");
}
System.out.println();
}
}
複製代碼
作者:
孫焌仁
時間:
2019-2-18 20:24
public class Ch1314
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,1.3);
Dog d2=new Dog("球球",1,1.2);
d1.showProfile();
d2.showProfile();
Cat c1=new Cat("咪咪",3,1.5);
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+"公斤.");
}
}
複製代碼
作者:
洪翊展
時間:
2019-2-18 20:25
public class Ch01
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,1.3,"紅棕");
Dog d2=new Dog("球球",1,1.3,"白");
Cat c1=new Cat("咪咪",3,1.5,"銀灰");
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
c1.shoProfile();
c1.makSound(3);
System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓.");
}
}
class Dog
{
static int sum;
int age,count;
double weight;
String color,name;
Dog(String name,int age,double weight,String color)
{
sum++;
this.age=age;
this.weight=weight;
this.color=color;
this.name=name;
}
void makeSound(int count)
{
for(int i=1;i<=count;i++)
{
System.out.print("汪~");
}
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲, 體重"+weight+"公斤,毛色為"+color+"色.");
}
}
class Cat
{
static int sum;
int age1,count1;
double weight1;
String color1,name1;
Cat(String name1,int age1,double weight1,String color1)
{
sum++;
this.age1=age1;
this.weight1=weight1;
this.color1=color1;
this.name1=name1;
}
void shoProfile()
{
System.out.println(name1+"今年"+age1+"歲, 體重"+weight1+"公斤,毛色為"+color1+"色.");
}
void makSound(int count1)
{
for(int i=1;i<=count1;i++)
{
System.out.print("喵~");
}
}
}
複製代碼
作者:
洪翊庭
時間:
2019-2-18 20:26
public class Ch06
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,1.3,"紅棕");
Dog d2=new Dog("球球",1,1.2,"白");
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
Cat c1=new Cat("咪咪",3,1.5,"銀灰");
c1.showProfile();
c1.makeSound(5);
System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓.");
}
}
class Dog
{
static int sum=0;
int age,count;
double weight;
String color,name;
Dog(String name,int age,double weight,String color)
{
sum++;
this.age=age;
this.weight=weight;
this.color=color;
this.name=name;
}
void makeSound(int count)
{
for(int i=1;i<=count;i++)
{
System.out.print("汪~");
}
System.out.println();
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲, 體重"+weight+"公斤,毛色為"+color+"色.");
}
}
class Cat
{
static int sum=0;
int age,count
;
double weight;
String color,name;
Cat(String name,int age,double weight,String color)
{
sum++;
this.age=age;
this.weight=weight;
this.color=color;
this.name=name;
}
void makeSound(int count)
{
for(int i=1;i<=count;i++)
{
System.out.print("喵~");
}
}
void showProfile()
{
System.out.println(name+"今年"+age+"歲, 體重"+weight+"公斤,毛色為"+color+"色.");
}
}
複製代碼
作者:
黃宇瑄
時間:
2019-2-18 20:27
public class Ch62
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,1.3);
Dog d2=new Dog("球球",1,1.2);
d1.showProfile();
d2.showProfile();
Cat c1=new Cat("咪咪",3,1.5);
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+"公斤.");
}
}
複製代碼
作者:
黃宇綸
時間:
2019-2-18 20:28
public class Ch01
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,1.3);
Dog d2=new Dog("球球",1,1.2);
d1.showProfile();
d2.showProfile();
Cat c1=new Cat("咪咪",3,1.5);
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+"公斤.");
}
}
複製代碼
作者:
王騰立
時間:
2019-2-18 20:32
public class Ch70
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,1.3);
Dog d2=new Dog("球球",1,1.2);
d1.showProfile();
d2.showProfile();
Cat c1=new Cat("咪咪",3,1.5);
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+"公斤.");
}
}
複製代碼
作者:
戴嘉禾
時間:
2019-2-18 20:34
public class Abc
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,1.3);
Dog d2=new Dog("球球",1,1.2);
d1.showProfile();
d2.showProfile();
Cat c1=new Cat("咪咪",3,1.5);
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+"公斤.");
}
}
複製代碼
作者:
may
時間:
2019-2-18 20:38
public class Ch54
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,1.3,"紅棕色");
Dog d2=new Dog("球球",1,1.2,"白色");
d1.showProfile();
d1.makeSound(2);
d2.showProfile();
d2.makeSound(3);
Cat c1=new Cat("咪咪",3,1.5,"銀灰色");
c1.ShowproProfile();
c1.makeSound(5);
System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓。");
}
}
class Dog
{
static int sum=0;
String name, color;
int age;
double w;
Dog(String n, int a, double 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,color;
int age;
double w;
Cat(String n,int a,double w,String c)
{
sum++;
name = n;
age = a;
this.w =w;
color=c;
}
void ShowproProfile()
{
System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
}
void makeSound(int n)
{
for(int i=1; i<=n; i++)
System.out.print("瞄~");
System.out.println();
}
}
複製代碼
作者:
啓銓
時間:
2019-2-22 20:27
public class Ch62
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,1.3);
Dog d2=new Dog("球球",1,1.2);
d1.showProfile();
d2.showProfile();
Cat c1=new Cat("咪咪",3,1.5);
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+"公斤.");
}
}
複製代碼
作者:
啓銓
時間:
2019-6-27 00:00
public class Ch62
{
public static void main(String[] args)
{
Dog d1=new Dog("憨憨",2,1.3);
Dog d2=new Dog("球球",1,1.2);
d1.showProfile();
d2.showProfile();
Cat c1=new Cat("咪咪",3,1.5);
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+"公斤.");
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2