标签:运算符 string类 start 常用方法 构造 ret 余额 怎么 char
package text;
public class Rectangle {
private double width; //声明属性
private double height;
private String color;
public Rectangle(double width,double height,String color) {
this.setWidth(width);
this.setHeight(height);
this.setColor(color);
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getArea() { //面积公式
return width*height;
}
public double getLength() { //周长
return (width+height)*2;
}
public static void main(String agrs[]) {
Rectangle rec1 = new Rectangle(30,40,"黑色");
System.out.println("长:"+ rec1.width+" 宽 "+ rec1.height+" 颜色: "+ rec1.color);
System.out.print("面积:" + rec1.getArea() + " 周长:" + rec1.getLength());
}
}
package text;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Account {
private String name; //姓名
private String date; //开户日期
private String code; //密码
private double save; //存钱金额
private double balance; //余额
private String bsf; //标识符
private double yc; //预存
public Account(String name,String code,double yc,double save){
this.setName(name);
this.setCode(code);
this.setSave(save); //构造方法
this.setYc(yc);
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //开户日期生成
date = dateFormat.format(now);
SimpleDateFormat dateFormat2 = new SimpleDateFormat("mmmddhhssmm"); //标识符生成
bsf = dateFormat2.format(now);
}
//给出set与get方法
public void setName(String name) {
this.name=name;
}
public String getName(){
return name;
}
public void setCode(String code) {
this.code=code;
}
public String getCode() {
return code;
}
public void setSave(double save) {
this.save=save;
}
public void setYc(double yc) {
this.yc=yc;
}
public double getBalance() {
return balance=yc+save;
}
public double getYc() {
return yc;
}
//主方法
public static void main(String[] agrs) {
Scanner sc = new Scanner(System.in);
String c = sc.nextLine(); //输入姓名
String b = sc.nextLine(); //设置密码
double n =sc.nextDouble(); //预存金额
double m = sc.nextDouble(); //本次存钱
Account ac = new Account(c,b,n,m); //构造对象
System.out.println( "开户日期:" + ac.date+"\n" +"姓名:" + ac.name+"\n" + "密码:" + ac.code );
System.out.println("预存为:"+ ac.yc);
System.out.println("本次存入金额为:" + ac.save);
System.out.println("余额:" + ac.getBalance() );
System.out.print("标识符 " + ac.bsf);
}
}
运行截图:
String str="hello";//直接赋值的方式
String str=new String("hello");//实例化的方式
int length():获取字符串的长度,其实也就是字符个数
char charAt(int index):获取指定索引处的字符
int indexOf(String str):获取str在字符串对象中第一次出现的索引
String substring(int start):从start开始截取字符串
String substring(int start,int end):从start开始,到end结束截取字符串。包括start,不包括end
char[] toCharArray():把字符串转换为字符数组
String toLowerCase():把字符串转换为小写字符串
String toUpperCase():把字符串转换为大写字符串
String trim():去除字符串两端空格
String[] split(String str):按照指定符号分割字符串
标签:运算符 string类 start 常用方法 构造 ret 余额 怎么 char
原文地址:https://www.cnblogs.com/xudo/p/11560125.html