类和对象练习

1.
public class Point {
    int x;
    int y;
    double getDistance(){
        double distance=Math.sqrt(x*x+y*y);
        return distance;
    }
    public static  void main(String[] args){
        Point p = new Point();
        p.x=3;
        p.y=4;
        System.out.print(p.getDistance());
    }
}


2.

 public class FindWrong {

    public static void main(String[] args) { int i=1, j; //j没有初始化
        float f1 = 0.1, f2 = 123;  //0.1后面应该加f 或者 前面加(float)
        final double PI = 3.1415926;
        char class = 'b'//class 是关键字
        i=j+5;
        f1=f2+2;
        PI=PI*2; //final不能改变
        z ='c';
    }}

3.

public class Point {
    int x;
    int y;
    public Point(int x1,int y1){
        x=x1;
        y=y1;
    }
    double getDistance(){
        double distance=Math.sqrt(x*x+y*y);
        return distance;
    }
    public static  void main(String[] args){
    Point p =new Point(3,4);
    System.out.println(p.getDistance());
    }
}


4.
import java.awt.*;
import java.sql.Statement;

public class Line {
    Point startPoint;
    Point endPoint;
    public Line(Point p1,Point p2){
        startPoint = p1;
        endPoint = p2;
    }
    public Line(int x1,int y1,int x2,int y2){
       startPoint = new Point(x1,y1);
       endPoint = new Point(x2,y2);
    }
    double getLength(){
        int x = startPoint.x-endPoint.x;
        int y = startPoint.y-endPoint.y;
        double length=Math.sqrt(x*x+y*y);
        return length;
    }
    public static void main(String[] args){
        Point p1 = new Point(1,1);
        Point p2 = new Point(2,2);
        Line line1 = new Line(p1,p2);
        System.out.print(line1.getLength());
    }

}


5.

public class Student {
    int age;
    String name;
    static int count=0;
    public  Student(){
        System.out.println("调用无参的构造方法");
        count++;
    }
    public Student(int age){
this.age=age;
        System.out.println("调用只有一个参数的构造方法");
        this.age=age;
    }
    public  Student(int age,String name){
this.age=age;
this.name=name;
        System.out.println("调用有两个参数的构造方法");
    }
    public void printlnfo(){
        System.out.println(this.name+"的年龄是"+this.age);
    }
    public static void printCount(){
        System.out.println("当前Count = : " + count);
    }
    public static void main(String[] ages){
        Student s1 = new Student(20,"张三");
        Student s2 = new Student(20,"张三");
        s1.printlnfo();
        s2.printlnfo();
        //Student s3 = new Student(); 测试count
        Student.printCount();
    }
}


6.

public class Account {
    String account;
    String name;
    double balance;
    double money;
    double money1;
    static double minBalance=10;

   public  Account(String a,String n,double b)
    {
       account = a;
       name = n;
       balance =b;
    }
    public  void deposit(double money){
        this.balance+=money;
        System.out.println("原有金额为:"+(balance-money)+"现存金额为:"+money+"存入后的金额为:"+balance);
    }
    public void withdraw(double money1){
        if((this.balance-money1)<minBalance)
            System.out.println("提出金额失败!");
        else
            System.out.println("原有金额为:"+(balance)+"现存金额为:"+money1+"取出后的金额为:"+(balance-money1));
    }
    public static void setMinbalance(double minBalance) {
        Account.minBalance = minBalance;
    }
    public  static  void main(String[] args){
        Account people= new Account("77777","cxa",77777);
        //存钱
        people.deposit(33333);
        //取钱
        people.withdraw(33333);
        //提出金额失败
        people.withdraw(8888888);
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值