zoukankan      html  css  js  c++  java
  • 20 带参构造方法(this关键字)

    Cat.java:

    package com.wys.javaoop;
    
    import javax.naming.NamingEnumeration;
    
    /**
     * 宠物猫类
     * @author snape
     */
    public class Cat {
        //成员属性:
        String name;    //昵称    默认值为null
        int month;      //年龄    默认值为0
        double weight;  //体重    默认值为0.0
        String species; //品种    默认值为null
    
        //成员方法:
        public Cat() {
        }
    
        //带参构造方法
        public Cat(String name, int month, double weight, String species){
            this.name = name;
            this.month = month;
            this.weight = weight;
            this.species = species;
        }
    
        //跑动方法
        public void run(){
            System.out.println("猫跑动");
        }
    
        //跑动方法,重载
        public void run(String name){
            System.out.println(name + "跑动");
        }
    
        //吃东西方法
        public void eat(){
            System.out.println("猫吃东西");
        }
    
    }

    CatTest.java:

    package com.wys.javaoop;
    
    import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
    
    public class CatTest {
        public static void main(String[] args) {
            Cat one = new Cat("mimi", 2, 3000, "英国短毛猫");
    
            System.out.println("名字:" + one.name);
            System.out.println("年龄:" + one.month);
            System.out.println("体重:" + one.weight);
            System.out.println("品种:" + one.species);
        }
    }

    结果:

  • 相关阅读:
    fibnacci数列递归实现
    求最大公约数伪代码
    第五周学习总结
    第四周学习总结
    我的黑客偶像
    BASE64编码
    第三周学习总结
    第二周学习总结
    Python gui
    SSH实践
  • 原文地址:https://www.cnblogs.com/CPU-Easy/p/12146565.html
Copyright © 2011-2022 走看看