zoukankan      html  css  js  c++  java
  • 定义类、实例化对象

    //类和对象:
            /*类是模子,确定对象将会拥有的特征(属性)和行为(方法)
             * 对象时类的实例表现。
             * 类是对象的类型
             * 对象时特定类型的数据
             * 
             * 属性和方法:
             * 属性:对象具有的各种静态特征
             * 对象有什么
             * 方法:对象具有的各种动态行为
             * 对象能做什么
             * 
             * */
            //1.什么是对象
            
            //2.什么是面向对象
            
            //3.什么是类
            
            //4.类和对象的关系

    定义类、实例化对象的例子:

    package com.imooc.animat;
    
    public class Cat {
        //成员属性:昵称、年龄、体重、品种:
        String name; //昵称 String类型默认值:null
        int month; //年龄 int类型默认值0
        double weight;//体重 double类型默认值:0.0
        String species; //品种
        
        //成员方法:跑动、吃东西
        public void run() {
            System.out.println("小猫快跑");
        }
        public void run(String name) {
            System.out.println(name+"快跑");
        }
        public void eat() {
            System.out.println("小猫吃鱼");
        }    
        
        public static void main(String[] args) {
            // TODO 自动生成的方法存根
    
        }
    }
    package com.imooc.animat;
    
    public class CatTest {
    
        public static void main(String[] args) {
            //对象实例化:
            Cat one=new Cat();
            //通过对象调用相应的属性和方法:
            one.eat();
            one.run();
            one.month=2;
            one.name="花花";
            one.weight=1000;
            System.out.println("年龄:"+one.month);
            System.out.println("昵称:"+one.name);
            System.out.println("体重:"+one.weight);
            System.out.println("品种:"+one.species);
            one.run(one.name);
        }
    
    }

  • 相关阅读:
    11111 Generalized Matrioshkas
    Uva 442 Matrix Chain Multiplication
    Uva 10815 Andy's First Dictionary
    Uva 537 Artificial Intelligence?
    Uva 340 MasterMind Hints
    SCAU 9508 诸葛给我牌(水泥题)
    Uva 10420 List of Conquests(排序水题)
    Uva 409 Excuses, Excuses!
    10/26
    11/2
  • 原文地址:https://www.cnblogs.com/yiweiyihang/p/12008898.html
Copyright © 2011-2022 走看看