zoukankan      html  css  js  c++  java
  • 简单的方法调用案例3

    2、写一个长方形的类,类中的属性:长方形的长,宽。类中有方法:
    (一)移动的方法,没有返回值,要求输出“长方形在移动”。
    (二)变化的方法,没有返回值,要求输出“长方形正在变化”。
    (三)显示此长方形信息的方法。
    (四)得到长方形的长的方法,返回长。
    (五)得到长方形宽的方法,返回宽。
    (六)得到长方形周长的方法。返回此长方形的周长。
    (七)得到长方形的面积的方法,返回此长方形面积。

    public class Rectangle {
    	//创建属性
    	int length;
    	int wide;
    	//(一)移动的方法,没有返回值,要求输出“长方形在移动”。
    	public void method1(){
    		System.out.println("长方形在移动");
    		
    	}
    	//(二)变化的方法,没有返回值,要求输出“长方形正在变化”。
        public void method2(){
        	System.out.println("长方形正在变化");
    		
    	}
        //(四)得到长方形的长的方法,返回长。
        public int method3(int a){
        	return a;
        }
        //(五)得到长方形宽的方法,返回宽。
        public int method4(int b){
        	return b;
        }
        //(六)得到长方形周长的方法。返回此长方形的周长。
        public int area(int c){
        	return c;
        }
        //(七)得到长方形的面积的方法,返回此长方形面积。
        public int peri(int d){
        	return d;
        }
    
    
    
    
    
    }
    

      

    public class demo2 {
    	public static void main(String[] args) {
    		//创建
    		Rectangle rec=new Rectangle();
    		//调用方法1,2
    		rec.method1();
    		rec.method2();
    		//初始化长方形长,并传参数给方法3
    		int len=3;
    		int a=rec.method3(len);
    		System.out.println("长方形的长为:"+a);
    		//初始化长方形宽,并传参数给方法4
    		int wide=4;
    		int b=rec.method4(wide);
    		System.out.println("长方形的宽为:"+b);
    		//初始化面积,传参并返回
    		int area=len*wide;
    		int c=rec.area(area);
    		System.out.println("长方形的面积为:"+c);
    		//同上
    		int peri=2*(len+wide);
    		int d=rec.peri(peri);
    		System.out.println("长方形的周长为:"+d);
    		
    		
    	}
    
    }
    

      

  • 相关阅读:
    23、Linux实操篇——RPM与YUM
    22、Linux实操篇——进程管理(重点)
    21、Linux实操篇——网络配置
    20、Linux实操篇——磁盘分区、挂载
    19、Linux实操篇——组管理和权限管理
    18、实操篇——实用指令
    17、实操篇——用户管理
    UVALive 2521 Game Prediction 题解
    UVALive 2517 Moving Object Recognition(模拟)
    UVALive 2324 Human Gene Functions(动态规划)
  • 原文地址:https://www.cnblogs.com/www-x/p/8087830.html
Copyright © 2011-2022 走看看