Chinaunix首页 | 论坛 | 博客
  • 博客访问: 345273
  • 博文数量: 81
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 847
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-25 22:29
个人简介

执一不失,能君万物http://weidian.com/s/284520723?wfr=c

文章分类

全部博文(81)

文章存档

2016年(11)

2015年(70)

我的朋友

分类: Java

2015-07-15 18:17:01

1.利用面向对象的编程方法,设计类Circle计算圆的面积

点击(此处)折叠或打开

  1. //利用面向对象的编程方法,设计类Circle计算圆的面积



  2. public class TestCircle {
  3.     public static void main(String[] args) {
  4.         Circle c1 = new Circle();
  5.         c1.Radius = 2;
  6.         double Area = c1.findArea();
  7.         System.out.println(Area);
  8.     }
  9. }


  10. public class Circle {
  11.     
  12.         int Radius;
  13.         
  14.         public double findArea(){
  15.             return 3.14 * Radius * Radius;
  16.             }
  17. }
2.题目

+ 表示修饰符为 public


点击(此处)折叠或打开

  1. public class TestPerson {
  2.     public static void main(String[] args) {
  3.     
  4.         Person p1 = new Person();
  5.         Person p2 = new Person();
  6.         p1.name = "Lily";
  7.         p1.age = 22;
  8.         p1.sex = 1;
  9.         
  10.         p2.name = "Lucy";
  11.         p2.age = 23;
  12.         p2.sex = 0;
  13.         
  14.         p1.showAge();
  15.         p1.study();
  16.         p1.addAge(5);
  17.         p1.showAge();
  18.         p1.study();
  19.         
  20.         System.out.println("---------------");
  21.         
  22.         p2.showAge();
  23.         p2.study();
  24.         p2.addAge(5);
  25.         p2.showAge();
  26.         p2.study();
  27.     }
  28. }


  29. public class Person {
  30.     public String name;
  31.     public int age;
  32.     public int sex;    

  33. public void study(){
  34.     System.out.println("Studying");
  35.     };
  36. public void showAge(){
  37.     System.out.println("age:"+ age);
  38.     };
  39. public void addAge(int i){
  40.     age = age + i;
  41.     };
  42. }


阅读(1585) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~