zoukankan      html  css  js  c++  java
  • P168 实战练习(构造方法)

    尝试编写一个矩形类,将长宽做为矩形类的属性,在构造方法中将长宽初始化,定义一个成员方法求此矩形的面积。

    编写代码如下:

    创建Rectangular类,则相关代码为:

     1 package org.hanqi.pn0120;
     2 
     3 public class Rectangular {
     4     
     5     
     6     double L;
     7     double W;
     8     Rectangular(double l,double w)
     9     {
    10         L=l;
    11         W=w;
    12     }
    13     double area()
    14     {
    15         return(L*W);
    16     }
    17   }
    18     

    再创建Test2类,则相关代码为:

     1 package org.hanqi.pn0120;
     2 
     3 public class Test2 {
     4 
     5     public static void main(String[] args) {
     6         
     7         Rectangular myRectangular= new Rectangular(4,5);
     8         System.out.println("这个矩形的面积="+myRectangular.area());
     9     }
    10 }

    运行结果是:

  • 相关阅读:
    Python 简单总结
    Python 简单总结
    Python 简介
    Python基础题
    Python基础题
    tDQSS
    parameter–precharge, tRCD and tRAS
    parameter–key parameters
    parameter -- tWR
    命令集
  • 原文地址:https://www.cnblogs.com/hanazawalove/p/5243350.html
Copyright © 2011-2022 走看看