学习笔记2(类初步)

一、构造器

1、构造器和类有相同的名字

2、构造器的头文件没有返回值,甚至没有void,因为构造器不被显式调用,也没有返回值。

3、example

public class Rectangle {
        private double length;
        private double width;
        
        public Rectangle(double len,double w){
            length=len ;
            width=w;
        }
    }

//This is how you name a constructor
//AccessSpecifier Classname(parameter...)
//This is how you use a constructor
//Rectangle box=new Rectangle(7.0,14.0)

注意!!!构造器里面一定不要写数据类型(length前不要加double),构造器外一定要有申明!!!

4、UML图(注意没有返回值)

 5、默认构造器

(1)当我们不写构造器的时候,在类编译的时候,java会默认地提供一个构造器,也就是默认构造器,默认构造器不接受任何参数,它将所有值设置为0,null和false

(2)eg:

// We wrote no constructor for the Rectangle class.
Rectangle r = new Rectangle(); // Calls the default constructor
// Now we wrote our own constructor for the Rectangle class.
Rectangle box = new Rectangle(); // Error! Must now pass arguments

6、无参数构造器

public Rectangle()
{
 length = 1.0;
 width = 1.0;
}
// Now we have written our own no-arg constructor.
Rectangle r = new Rectangle(); // Calls the no-arg constructor

这个构造器会在不接受参数的前提下,将rectangle的两个参数设置为1和1

二、重载

1、java使用方法的签名来识别方法,签名包括方法的名称以及接受的参数,方法的返回值并不包含在方法的签名中

2、重载构造器

eg:

public Rectangle()
{
 length = 0.0;
 width = 0.0;
}
public Rectangle(double len, double w)
{
 length = len;
 width = w;
}

The following code shows an example of how each constructor is called

Rectangle box1 = new Rectangle();
Rectangle box2 = new Rectangle(5.0, 10.0);

Java仅在不为类编写任何构造函数时才提供默认构造函数。如果一个类具有接受参数的构造函数,但它没有无参数构造函数,你就不能用第一种方式实例化rectangle了。

当你写一个接受参数的类的构造函数时,如果你还想可以用第一种方式实例化类,也可以多写一个无参数构造函数。

3、例子

 /**
  The BankAccount class simulates a bank account.
  */
 
  public class BankAccount
  {
  private double balance; // Account balance
 
  /**
 This constructor sets the starting balance
 at 0.0.
 */

  public BankAccount()
 {
 balance = 0.0;
 }

  /**
 This constructor sets the starting balance
 to the value passed as an argument.
 @param startBalance The starting balance.
 */

  public BankAccount(double startBalance)
 {
 balance = startBalance;
 }

  /**
 This constructor sets the starting balance
 to the value in the String argument.
 @param str The starting balance, as a String. */

  public BankAccount(String str)
 {
 balance = Double.parseDouble(str);
 }

  /**
 The deposit method makes a deposit into
 the account.
 @param amount The amount to add to the
 balance field.
 */

  public void deposit(double amount)
 {
 balance += amount;
 }

  /**
 The deposit method makes a deposit into
 the account.
 @param str The amount to add to the
 balance field, as a String.
 */

  public void deposit(String str)
 {
 balance += Double.parseDouble(str);
 }

  /**
 The withdraw method withdraws an amount
 from the account.
 @param amount The amount to subtract from the balance field.
 /

  public void withdraw(double amount)
 {
 balance -= amount;
 }

  /**
 The withdraw method withdraws an amount
 from the account.
 @param str The amount to subtract from
 the balance field, as a String.
 */

  public void withdraw(String str)
 {
 balance -= Double.parseDouble(str);
 }

  /**
 The setBalance method sets the account balance.
 @param b The value to store in the balance field.
 */

  public void setBalance(double b)
 {
 balance = b;
 }

  /**
 The setBalance method sets the account balance.
 @param str The value, as a String, to store in
 the balance field.
 */

 public void setBalance(String str)
 {
 balance = Double.parseDouble(str);
 }

/**
 The getBalance method returns the
 account balance.
 @return The value in the balance field.
 */

 public double getBalance()
 {
 return balance; }
 }

 

三、包

1、关系

java API是java提供的一系列类的总称,而包则是一些相关的,有联系的类的集合。很多的包并不能直接使用,你需要先用关键词import去申明这个类在哪个包里,例如:

import java.util.Scanner;

申明了Scanner类

2、申明方法

第一种如上图所示

第二种

import java.util.*;

“ * ”表明申明了util这个包中所有的类(Scanner类,Random类等等都在这个包里)

ps:使用这种方法并不影响程序的大小,它只是告诉编译器你希望使这个包中所有的类都可用

3、java.lang Package以及其他包

(1)java.lang Package

这个类中包含了一些最常用的包,比如System,String,在每个java程序中,这个包会自动申明,所以可以直接使用这些类的方法

(2)其他包

四、一个小作业

Coin Toss Simulator
Write a class named Coin . The Coin class should have the following field:
A String named sideUp . The sideUp field will hold either “heads” or “tails” indicating the side of the coin
that is facing up.
The Coin class should have the following methods:
A no-arg constructor that randomly determines the side of the coin that is facing up (“heads” or “tails”) and
initializes the sideUp field accordingly.
A void method named toss that simulates the tossing of the coin. When the toss method is called, it
randomly determines the side of the coin that is facing up (“heads” or “tails”) and sets the sideUp field
accordingly.
A method named getSideUp that returns the value of the sideUp field.
Write a program that demonstrates the Coin class. The program should create an instance of the class and
display the side that is initially facing up. Then, use a loop to toss the coin 20 times. Each time the coin is tossed,
display the side that is facing up. The program should keep count of the number of times heads is facing up and
the number of times tails is facing up, and display those values after the loop finishes.
public class demoCoin {
    public static void main (String[] args){
        Coin a=new Coin();
        int heads=0;
        int tails=0;
        System.out.println(a.getSideUp());
        for(int i=1;i<20;i++){
            a.toss();
            if(a.getSideUp().equals("heads"))
                heads++;
            else
                tails++;
        }
        System.out.println(tails+" "+heads);
    }
}
import java.util.Random;

public class Coin {
    public String sideUp;
    public Coin(){
        Random a=new Random();
        double b=a.nextDouble();
        if(b<0.5)
            sideUp="heads";
        else
            sideUp="tails";
    }
    public void toss(){
        Random a=new Random();
        double b=a.nextDouble();
        if(b<0.5)
            sideUp="heads";
        else
            sideUp="tails";
    }

    public String getSideUp() {
        return sideUp;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值