【参考答案】java基础练习: 类(对象属性、对象数组)、构造方法及this、方法重载、对象初始化
类
创建一个用户类,为用户设置年龄(0-120),如果设置的不合法,给出提示
Person.java
package com.qzcsbj; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { if(age<0 || age>130){ System.out.println("年龄范围只能是0-130,您输入的是:" + age); return; } this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
TestPerson.java
package com.qzcsbj; import java.util.Scanner; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class Test { public static void main(String[] args) { Person p = new Person(); p.setAge(139); System.out.println("age = " + p.getAge()); Person p2 = new Person(); System.out.println("age = " + p2.getAge()); } }
定义Boy、Girl类,对象boy给对象girl求婚,且girl答复;要求,boy对象的求婚方法传入girl对象,girl对象的答复方法传入求婚的对象
package com.qzcsbj; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class Test { public static void main(String[] args) { Boy boy = new Boy("jack"); Girl girl = new Girl("lucy"); boy.marry(girl); } } class Boy { private String name; public Boy(String name){ this.name = name; } public String getName() { return name; } public void marry(Girl girl){ System.out.println(girl.getName() + ",你愿意嫁给我码?"); girl.answer(this); } } class Girl { private String name; public Girl(String name){ this.name = name; } public String getName() { return name; } public void answer(Boy boy){ System.out.println(boy.getName() +",你有房么?有车么?"); } }
分别用面向过程和面向对象的思维,计算圆的面积;
package com.qzcsbj; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class Test { public static void main(String[] args) { // 面向过程 System.out.println("圆的面积是:"+ getArea(3)); // 面向对象 Circle c = new Circle(); c.setRadius(3); System.out.println("圆的面积是:"+ c.getArea()); } public static double getArea(int r){ return 3.14 * r * r; } } class Circle { private int radius; public int getRedius(){ return radius; } public void setRadius(int radius) { if(radius <= 0){ System.out.println("redius 赋值不合法: " + radius + ", " + "redius 取默认的初始值 0"); return; } this.radius = radius; } public double getArea(){ return 3.14 * radius * radius; } }
全局变量、局部变量、this
写代码演示为什么要用this,解决了什么问题?
package com.qzcsbj; public class Test { String name = "jack"; public void modifyName(String username){ name = username + "666"; System.out.println(name); } public void modifyName2(String name){ name = name + "666"; System.out.println(name); } public void modifyName3(String name){ this.name = name + "666"; System.out.println(name); } public static void main(String[] args) { Test test = new Test(); // test.modifyName("tom"); // tom666 // System.out.println(test.name); // tom666 // System.out.println("==============="); // test.modifyName2("tom"); // tom666 // System.out.println(test.name); // jack System.out.println("==============="); test.modifyName3("tom"); // tome System.out.println(test.name); // tom666 } }
下面结果分别是?
类中,jvm给成员变量赋了初始值,不会给局部变量赋初始值,局部变量没有默认值
public class Test { static String str; public static void main(String[] args) { static String str; System.out.println(str); } }
public class Test { static String str; public static void main(String[] args) { String str; System.out.println(str); } }
public class Test { static String str; public static void main(String[] args) { System.out.println(str); } }
对象属性
创建Person类(name,age,address),address是对象属性,Address类(province、city),创建一个Person类对象person,并给所以属性赋值,最后打印person
package com.qzcsbj; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class Test { public static void main(String[] args) { Person person = new Person("jack",22); Address add = new Address(); add.setProvince("福建省"); add.setCity("厦门市"); person.setAddress(add); System.out.println("name:" + person.getName()); System.out.println("age: " +person.getAge()); System.out.println("省份: " +person.getAddress().getProvince()); System.out.println("城市:" + person.getAddress().getCity()); } } class Person{ private String name; private int age; private Address address; public Person(){ super(); } public Person(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } } class Address{ private String province; // 省 private String city; // 市 public Address() { } public Address(String province, String city) { this.province = province; this.city = city; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
对象数组
定义学生类(姓名、学号),录入学生信息,并将学生保存到数组中
package com.qzcsbj; public class Student { private String name; private int stuNo; public Student() { } public Student(String name, int stuNo) { this.name = name; this.stuNo = stuNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getStuNo() { return stuNo; } public void setStuNo(int stuNo) { this.stuNo = stuNo; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", stuNo='" + stuNo + '\'' + '}'; } }
package com.qzcsbj; import java.util.Scanner; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class Test { public static void main(String[] args) { Student[] students = new Student[2]; inputStudentInfo(students); getStudentInfo(students); } private static void getStudentInfo(Student[] students) { System.out.println("姓名\t学号"); System.out.println("\n-------------学生信息-------------"); for (Student student : students) { System.out.println(student.getName() + "\t" + student.getStuNo()); } } private static void inputStudentInfo(Student[] students) { Scanner input = new Scanner(System.in); for (int i = 0; i < students.length; i++) { students[i] = new Student(); // 实例化对象 System.out.println("=============请输入第" + (i + 1) + "个学生的信息================"); System.out.print("请输入姓名:"); students[i].setName(input.next()); System.out.print("请输入学号:"); students[i].setStuNo(input.nextInt()); } } }
构造方法、this、方法重载
定义一个类(三个属性),三参构造方法中,调用两参构造方法
package com.qzcsbj; public class Person { private String name; private int age; private String phone; public Person() { System.out.println("无参"); } public Person(String name) { System.out.println("一个参数"); this.name = name; } public Person(String name, int age) { this(); System.out.println("两个参数"); this.name = name; this.age = age; } public Person(String name, int age, String phone) { this(name,age); System.out.println("三个参数"); // this.name = name; // this.age = age; this.phone = phone; } public void study(String type){ System.out.println(this.name + "的学习方式是:" + type); } public void study(String type,String subject){ System.out.println(this.name + "的学习方式是:" + type + ", 学习的科目是:" + subject); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", phone='" + phone + '\'' + '}'; } }
对象初始化顺序
写代码验证对象初始化顺序:代码块(静态、非静态)、构造
package com.qzcsbj; public class User { public User(){ System.out.println(name); System.out.println("构造方法。"); this.name = "性能测试"; } public void a(){ System.out.println("实例方法。"); } public static void b(){ System.out.println("静态方法。"); } { System.out.println("代码块。"); name = "qzcsbj"; } String name = "全栈测试笔记"; // 实例属性 static String job = "tester"; // 静态属性 static{ System.out.println(job); System.out.println("静态代码块。"); // job = "QA"; } }
package com.qzcsbj; public class T { public static void main(String[] args) { // System.out.println(User.job); // User.b(); User user = new User(); System.out.println(user.name); } }
tester
静态代码块。
代码块。
全栈测试笔记
构造方法。
性能测试
综合练习
练习1:定义一个Person类,要求:属性(name,age,phone),方法(study)并实现方法重载,实现封装,实例化多个对象,输出某个对象的信息,最后输出创建了多少对象
package com.qzcsbj; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class Person { private String name; private int age; private String phone; private static int obj_nums; public Person() { System.out.println("无参"); obj_nums++; } public Person(String name, int age, String phone) { System.out.println("有参"); this.name = name; this.age = age; this.phone = phone; obj_nums++; } public void study(String type){ System.out.println(this.name + "的学习方式是:" + type); } public void study(String type,String subject){ System.out.println(this.name + "的学习方式是:" + type + ", 学习的科目是:" + subject); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public static int getObj_nums() { return obj_nums; } public static void setObj_nums(int obj_nums) { Person.obj_nums = obj_nums; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", phone='" + phone + '\'' + '}'; } }
测试类
package com.qzcsbj; import java.util.Scanner; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class Test { public static void main(String[] args) { Person p1 = new Person("jack",18,"13888888888"); System.out.println(p1.toString()); System.out.println("当前累计已经创建了" + Person.getObj_nums() + "个Person对象。"); System.out.println(p1.getName()); System.out.println(p1.getAge()); System.out.println(p1.getPhone()); Person p2 = new Person("tom",19,"13888888889"); System.out.println("当前累计已经创建了" + Person.getObj_nums() + "个Person对象。"); Person p3 = new Person("laowang",19,"13888888887"); System.out.println("当前累计已经创建了" + Person.getObj_nums() + "个Person对象。"); p3.study("book"); p2.study("video", "java"); Person p4 = new Person(); System.out.println("当前累计已经创建了" + Person.getObj_nums() + "个Person对象。"); } }
练习2:定义一个Card的扑克牌类的创建和洗牌
该类有两个private访问权限的字符串变量face和suit: 分别描述一张牌的牌面值(如:A、K、Q、J、10、9、…、3、2等)和花色(如:“黑桃”、“红桃”、“梅花”和“方块”)。 定义Card类中的public访问权限的构造方法,为类中的变量赋值; 定义protected访问权限的方法getFace(),得到扑克牌的牌面值; 定义protected访问权限的方法getSuit(),得到扑克牌的花色; 定义方法toString(),返回表示扑克牌的花色和牌面值字符串(如“红桃A”、“梅花10”等)。 (1)使用Card类的构造方法给deck数组的52张牌赋值,请写出实现上述功能的程序段。 (2)请编写模拟洗牌的程序段,即把数组deck中的扑克牌随机打乱存放顺序。
类
package com.qzcsbj; /** * @description : <描述> */ public class Card { private String face; // 牌面值 private String suit; // 花色 public Card() { } public Card(String face, String suit) { this.face = face; this.suit = suit; } protected String getFace() { return face; } protected String getSuit() { return suit; } public String toString(){ return face + suit; } }
实现
package com.qzcsbj; import java.util.Arrays; /** * @description : <描述> */ public class Test { public static void main(String[] args) { System.out.println(Math.random()); String[] s = new String[]{"黑桃", "红心", "梅花", "方块"}; String[] f = new String[]{"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; Card[] deck = new Card[52]; for (int i = 0; i < s.length; i++) { for (int j = 0; j < f.length; j++) { deck[i * 13 + j] = new Card(s[i], f[j]); } } System.out.println("洗牌前:"); System.out.println(Arrays.toString(deck)); // 随机洗牌 for(int i = 0;i < deck.length; i++) { // Math.random()大于等于0.0且小于1.0的伪随机double值[0.0, 1.0) // int num = (int)(Math.random()*(deck.length - 1)); // [0,51),取整[0,50] int num = (int)(Math.random()*deck.length); // [0,52),取整[0,51] Card temp = deck[i]; deck[i] = deck[num]; deck[num] = temp; } System.out.println("洗牌后:"); System.out.print(Arrays.toString(deck)); } }
【java百题计划汇总】
详见:https://www.cnblogs.com/uncleyong/p/15828510.html
原文会持续更新,原文地址:https://www.cnblogs.com/uncleyong/p/17043964.html
__EOF__
本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!