java类与方法

来源:互联网 发布:手机充值卡 非网络 编辑:程序博客网 时间:2024/06/07 01:34

今日看了看网上的java视频!哎呦  不错!

类与方法定义方法!

如何定义类?
修饰符 class 类的名字
{
//类的内容(包含了属性与方法)
}
方法:如何定义方法?
修饰符 返回类型 方法名称([参数1, 参数2, 参数3…])
{
//方法体
}

 

 

 

举例说明

求长方体的体积和表面积

public class Box
{
 public int tiji(int length,int width, int height)
 { return length*width*height; }

 public int biaomianji(int length,int width, int height)
 { return length*width+height*length+height*width+length*width+height*length+height*width; }


 public static void main(String[] args)
 {
  Box box=new Box();
  int a=5;
  int b=6;
  int c=8;
  
  int x=box.tiji(a,b,c);
  int y=box.biaomianji(a,b,c);

  System.out.println("体积为:"+x);
  System.out.println("表面积为:"+y);
 }

}

 

 

 

原创粉丝点击