public class Main {
public static void main(String[] args) {
//直接创建
animal animal0 = new animal();
//先声明在创建
animal animal1;
animal1 = new animal();
}
}
3.2 直接访问对象成员
注:入门暂时不考虑访问修饰符,static,final等关键字
3.2.1 访问对象的成员属性
访问属性为 对象名.属性名
System.out.print(animal0.name);
在创建对象时如果没有给属性没有赋值,会有初始值
System.out.println("animal Name is " + animal0.name); //null
System.out.println("animal Age is " + animal0.age); //0
System.out.println("animal Weight is " + animal0.weight);//0.0
System.out.println("animal height is " + animal0.height);
public class Main {
public static void main(String[] args) {
animal animal1 = new animal("tom",3,5.4f,0.53f);
//引用传递
animal animal2 = animal1;
animal2.InformationOutput();
/* animal Name is tom
animal Age is 3
animal Weight is 5.4
animal height is 0.53*/
animal1.InformationOutput();
/* animal Name is tom
animal Age is 3
animal Weight is 5.4
animal height is 0.53*/
animal.Clear(animal1);
animal1.InformationOutput();
/* animal Name is null
animal Age is 0
animal Weight is 0.0
animal height is 0.0*/
}
}
public static int sum(int ...num){
int sum = 0;
for(int i = 0;i < num.length ;i++){
sum += num[i];
}
return sum;
}
public static float sum(float ...num){
float sum = 0;
for(int i = 0;i < num.length ;i++){
sum += num[i];
}
return sum;
}
4.4 可变参数(不定参数)
语法:访问修饰符 返回类型 方法名(数据类型...形参名)
多个同名,同功能,参数个数不同的方法封装成一个方法
省略号表示接受的是可以是多个参数,使用时可以当做数组来使用。(本质就是数组)
可以和普通参数放在形参列表,但是可变参数必须放在最后,最多一个可变参数
public static int sum(int ...num){
int sum = 0;
for(int i = 0;i < num.length ;i++){
sum += num[i];
}
return sum;
}