掌握类的定义,熟悉属性、构造函数、方法的做用,掌握用类做为类型声明变量和方法返回值;
理解类和对象的区别,掌握构造函数的使用,熟悉经过对象名引用实例的方法和属性;
理解static修饰付对类、类成员变量及类方法的影响。java
一、写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具备:数组
(1) 使用构造函数完成各属性的初始赋值函数
(2) 使用get…()和set…()的形式完成属性的访问及修改学习
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法this
二、银行的帐户记录Account有帐户的惟一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,帐户密码(六位的数字,能够用0开头),当前的余额。银行规定新开一个帐户时,银行方面提供一个标识符、帐户初始密码123456,客户提供姓名,开户时客户能够直接存入一笔初始帐户金额,不提供时初始余额为0。定义该类,并要求该类提供以下方法:存款、取款、变动密码、能够分别查询帐户的标识、姓名、开户日期、当前余额等信息。加密
package o; public class a { public static void main(String[] args) { Rectangle re = new Rectangle(); re.get(); double a =re.getArea(); System.out.println(a); double b =re.getLength(); System.out.println(b); } } class Rectangle { double width; double heigh; String color; Rectangle() { this.width=3; this.heigh=4; this.color="Red"; } public void get(){ System.out.println("rec "+this.width); System.out.println("higher "+this.heigh); System.out.println("color "+this.color); } public void setwidth(double single) { this.width=single; } public void setheigh(double single) { this.heigh=single; } public void setcolor(String single) { this.color=single; } public double getArea() { return this.width*this.heigh; } public double getLength(){ return this.width*1+this.heigh*2; } }
package o; public class a { public static void main(String[] args) { Account re = new Account("boniu","hl",2019,9,20,"123456",2000); re.setAll(); re.changebalance(0.05); re.setAll(); re.changepsd("123456"); re.setAll(); } } class Account { String ID; String psd; int year; String name; double balance; int month; int day; Account() { this.ID="boniu"; this.name="null"; this.year=0; this.month=0; this.day=0; this.psd="null"; this.balance=0; } Account(String ID, String name, int year, int month, int day, String psd, double balance) { this.ID = ID; this.name = name; this.year = year; this.month = month; this.day = day; this.psd = psd; this.balance = balance; } public void changebalance(double single) { this.balance += single; } public void changepsd(String single) { this.psd = single; } public void setAll() { System.out.println(this.ID); System.out.println(this.name); System.out.println(this.year+"."+this.month+"."+this.day); System.out.println("balance:"+this.balance); } }
这周所学及小拓展:code
package o; import java.util.Scanner; public class a { public static void main(String args[]){ Scanner in=new Scanner(System.in); System.out.print("输入字符串:"); String str=in.next(); int i; char b[]=new char[50]; System.out.print("加密后为:"); for(i=0;i<str.length();i++) { b[i]=str.charAt(i); b[i]+=3; if(b[i]-'a'>25) b[i]-=26; System.out.print(b[i]); } } }
将输入的字符串中的每一个字符加3,输出,实现字串的加密,大于26的字符,减去26返回。对象
用来判断两个对象是否同样,对其所指向的地址也能判断是否相等,而“==”判等符号只能够判断两个对象的值是否相等。blog
public class StringEquals { public static void main(String[] args) { String s1=new String("Hello"); String s2=new String("Hello"); System.out.println(s1==s2); System.out.println(s1.equals(s2)); String s3="Hello"; String s4="Hello"; System.out.println(s3==s4); System.out.println(s3.equals(s4)); } }
当分别new两个对象时,这两个对象所指向的存储地址不同,开辟了新的存储单元,而“==”只能判断值是否相等,因此第一个会输出错误,而equals()判断两个对象是否彻底同样,包括值和地址,第二个输出true。当只String两个相同字符串时,这两个字符串指向的是同一个地址,即用“==”和equals()都输出true。字符串
CharAt() 获取指定位置字符
Length() 取字串的长度
GetChars() 获取制定未知的字串复制到字符串数组中。
toUpperCase(),toLowerCase)() 大小写的转换
trim() 去除头尾空格
Replace() 字串替换
toCharArray() 字符串转换为字符数组。
小总结:这周主要是“重”作了一下上次做业的7个题目,觉得能一鼓作气的写下去,结果边写边出现一片一片的红叉叉。在室友的帮助下我仍是完成了三个题目,为何看别人写的代码,看得懂,本身写的时候却老是错呢?...这周的题目我以为emmmm好像不是很难,由于我这样的菜鸡居然都能写出碎片化的代码了,虽然最终仍是靠着度娘和别人的代码填满了本身代码的窟窿吧。但我已心满意足,后面的学习过程当中慢慢长进吧!