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);
        }
    }

    结果:

  • 相关阅读:
    83. Remove Duplicates from Sorted List
    141. Linked List Cycle
    hdu1028 划分数
    XDU1019 阶乘因子的个数
    poj2773 容斥原理
    poj1091 容斥原理的应用
    poj1173 多重集组合数
    HDU 1465 错排问题
    poj 1496
    复习之求一个数的约束之积模一个质数
  • 原文地址:https://www.cnblogs.com/CPU-Easy/p/12146565.html
Copyright © 2011-2022 走看看