构造方法

构造方法

根据前面章节定义的Cat类,现在Cat类中添加带参的Cat方法,注意参数名不能和要赋值的变量名相同:

public Cat(String newName, int newMonth, double newWeight, String newSpecies){name = newName;month = newMonth;weight = newWeight;species = newSpecies;}

进行传参:

package java_animal;public class CatTest {public static void main(String[] args) {Cat one = new Cat("花花", 2, 1000, "英国短毛猫");System.out.println("昵称:"+one.name);System.out.println("年龄:"+one.month);System.out.println("体重:"+one.weight);System.out.println("品种:"+one.species);//one.run(one.name);}
}

输出:

昵称:花花
年龄:2
体重:1000.0
品种:英国短毛猫

下一节讲关于this关键字的用法。

发布者:admin,转转请注明出处:http://www.yc00.com/news/1687814378a47455.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信