• 简单的方法调用案例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);
    		
    		
    	}
    
    }
    

      

  • 相关阅读:
    数据库索引的作用和优势缺点
    Python 新浪微博元素 (Word, Screen Name)词汇多样性
    解决Myeclipse在port占用,导致tomcat无法启动。(Linux)
    linux命名管道通信过程
    Lua环境搭建之使用EditPlus搭建Lua开发环境
    详解LUA开发工具及其环境配置
    UltraEdit配置python和lua环境
    Lua学习笔记
    Linux 安装ibus极点五笔输入法备忘录
    win2k/xp查看当前进程
  • 原文地址:https://www.cnblogs.com/www-x/p/8087830.html
Copyright © 2020-2023  润新知