JavaSE系列代码03:以一般变量为参数的方法调用

Variables come from mathematics. They are abstract concepts that can store calculation results or represent values in computer language. Variables can be accessed by the variable name. In imperative languages, variables are usually mutable, but in pure functional languages such as Haskell, variables can be immutable. In some languages, variables may be defined as abstractions with variable states and storage space (such as in Java and Visual Basic); however, other languages may use other concepts (such as objects of C) to refer to such abstractions, rather than strictly defining the exact extension of “variables”.

class Cylinder
{
  double radius;
  int height;
  double pi;
  void SetCylinder(double r, int h, double p)   //具有3个参数的方法
  {
    pi=p;
    radius=r;
    height=h;
  }
  double area()
  {
    return pi* radius* radius;
  }
  double volume()
  {
    return area()*height; 
  }
}
public class Javase_03   //定义公共类
{
  public static void main(String[] args)
  {
    Cylinder volu=new Cylinder();
    volu.SetCylinder(2.5, 5,3.14);  //调用并传递参数到SetCylinder()方法
    System.out.println("底圆半径="+volu.radius);
    System.out.println("圆柱的高="+volu.height);
    System.out.println("圆周率pi="+volu.pi);
    System.out.print("圆柱");
    System.out.println("底面积="+volu.area());
    System.out.println("圆柱体体积="+volu.volume());
  }
}
发布了13 篇原创文章 · 获赞 160 · 访问量 8546

猜你喜欢

转载自blog.csdn.net/blog_programb/article/details/105376426