题目01
//命令行参数用法举例
package com.jerry.java; /** * @author jerry_jy * @create 2022-09-30 10:24 */ public class CommandPara {//命令行参数用法举例 public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.println("args[" + i + "] = " + args[i]); } } }
题目02
内部类使用,说出程序的运行结果: 在内部类Inner中s=100
package com.jerry.java; /** * @author jerry_jy * @create 2022-09-30 18:02 */ public class InnerTest { public static void main(String args[]) { Outer o = new Outer(); o.ma(); } } class Outer { private int s; public class Inner { public void mb() { s = 100; System.out.println("在内部类Inner中s=" + s); } } public void ma() { Inner i = new Inner(); i.mb(); } }
题目03
//静态代码块
package com.jerry.java; /** * @author jerry_jy * @create 2022-09-30 10:30 */ class Root {//静态代码块 static { System.out.println("Root的静态初始化块"); } { System.out.println("Root的普通初始化块"); } public Root() { System.out.println("Root的无参数的构造器"); } } class Mid extends Root { static { System.out.println("Mid的静态初始化块"); } { System.out.println("Mid的普通初始化块"); } public Mid() { System.out.println("Mid的无参数的构造器"); } public Mid(String msg) { //通过this调用同一类中重载的构造器 this(); System.out.println("Mid的带参数构造器,其参数值:" + msg); } } class Leaf extends Mid { static { System.out.println("Leaf的静态初始化块"); } { System.out.println("Leaf的普通初始化块"); } public Leaf() { //通过super调用父类中有一个字符串参数的构造器 super("尚硅谷"); System.out.println("Leaf的构造器"); } } public class LeafTest { public static void main(String[] args) { new Leaf(); //new Leaf(); } }
题目04
考察静态代码块,请说出下面程序的运行结果: Number of total is 0 Number of total is 1
package com.jerry.java; /** * @author jerry_jy * @create 2022-09-30 9:45 */ public class PersonTest { public static void main(String[] args) { System.out.println("Number of total is " + Person1.getTotalPerson());//0 //没有创建对象也可以访问静态方法 Person1 p1 = new Person1(); System.out.println("Number of total is " + Person1.getTotalPerson());//1 } } class Person1 { private int id; private static int total = 0; public static int getTotalPerson() { //id++; //非法 return total; } public Person1() { total++; id = total; } }
题目05
考察静态代码块,请说出下面程序的运行结果: 11111111111 44444444444 77777777777 ************************ 22222222222 33333333333 55555555555 66666666666 ************************ 22222222222 33333333333 55555555555 66666666666 ************************ 22222222222 33333333333
package com.jerry.java; /** * @author jerry_jy * @create 2022-09-30 10:33 */ class Father { static { System.out.println("11111111111");//静态代码块,只会执行一次 } { System.out.println("22222222222");//随着类的加载而加载,优先于构造器加载 } public Father() { System.out.println("33333333333"); } } public class Son extends Father { static { System.out.println("44444444444"); } { System.out.println("55555555555"); } public Son() { System.out.println("66666666666"); } public static void main(String[] args) { // 由父及子 静态先行 System.out.println("77777777777"); System.out.println("************************"); new Son(); System.out.println("************************"); new Son(); System.out.println("************************"); new Father(); } }
题目06
下面程序的运行结果是? 100 101
package com.jerry.java; /** * @author jerry_jy * @create 2022-09-30 9:34 */ /* 下面程序的运行结果是? */ public class StaticDemo { public static void main(String args[]) { Person.total = 100; // 不用创建对象就可以访问静态成员 //访问方式:类名.类属性,类名.类方法 System.out.println(Person.total);//100 Person c = new Person(); System.out.println(c.total); //输出101 } } class Person { //类变量应用举例 private int id; public static int total = 0; public Person() { total++; id = total; } public static void main(String args[]) { Person Tom = new Person(); Tom.id = 0; total = 100; // 不用创建对象就可以访问静态成员 } }
题目07
代理模式
package com.jerry.java; /** * @author jerry_jy * @create 2022-09-30 16:32 */ public class StaticProxyTest { public static void main(String[] args) { Star s = new Proxy(new RealStar()); s.confer(); s.signContract(); s.bookTicket(); s.sing(); s.collectMoney(); } } interface Star { void confer();// 面谈 void signContract();// 签合同 void bookTicket();// 订票 void sing();// 唱歌 void collectMoney();// 收钱 } class RealStar implements Star { public void confer() { } public void signContract() { } public void bookTicket() { } public void sing() { System.out.println("明星:歌唱~~~"); } public void collectMoney() { } } class Proxy implements Star { private Star real; public Proxy(Star real) { this.real = real; } public void confer() { System.out.println("经纪人面谈"); } public void signContract() { System.out.println("经纪人签合同"); } public void bookTicket() { System.out.println("经纪人订票"); } public void sing() { real.sing(); } public void collectMoney() { System.out.println("经纪人收钱"); } }
题目08
static测试
package com.jerry.java; /** * @author jerry_jy * @create 2022-09-29 22:14 */ public class StaticTest { public static void main(String[] args) { Circle1 c1 = new Circle1(2.0); Circle1 c2 = new Circle1(3.0); Circle1.getName(); c1.display(); c2.display(); } } class Circle1 { private double radius; public static String name = "这是一个圆"; public static String getName() { return name; } public Circle1(double radius) { this.radius = radius; } public double findArea() { return Math.PI * radius * radius; } public void display() { System.out.println("name:" + name + "radius:" + radius); } }
题目09
//抽象类的应用:模板方法的设计模式
package com.jerry.java; /** * @author jerry_jy * @create 2022-09-30 11:10 */ //抽象类的应用:模板方法的设计模式 public class TemplateMethodTest { public static void main(String[] args) { BankTemplateMethod btm = new DrawMoney(); // DrawMoney btm = new DrawMoney(); btm.process(); BankTemplateMethod btm2 = new ManageMoney(); // ManageMoney btm2 = new ManageMoney(); btm2.process(); } } abstract class BankTemplateMethod { // 具体方法 public void takeNumber() { System.out.println("取号排队"); } public abstract void transact(); // 办理具体的业务 //钩子方法 public void evaluate() { System.out.println("反馈评分"); } // 模板方法,把基本操作组合到一起,子类一般不能重写 public final void process() { this.takeNumber(); this.transact();// 像个钩子,具体执行时,挂哪个子类,就执行哪个子类的实现代码 this.evaluate(); } } class DrawMoney extends BankTemplateMethod { public void transact() { System.out.println("我要取款!!!"); } } class ManageMoney extends BankTemplateMethod { public void transact() { System.out.println("我要理财!我这里有2000万美元!!"); } }
题目10
//判断输出结果为何?
package com.jerry.java; /** * @author jerry_jy * @create 2022-09-30 18:10 */ //判断输出结果为何? public class Test { public Test() { Inner s1 = new Inner(); s1.a = 10; Inner s2 = new Inner(); s2.a = 20; Test.Inner s3 = new Test.Inner(); System.out.println(s3.a); } class Inner { public int a = 5; } public static void main(String[] args) { Test t = new Test();//5 Inner r = t.new Inner();//5 System.out.println(r.a); // String foo = args[1]; // String bar = args[2]; // String baz = args[3]; } }
题目11
/* 编写一个类实现银行账户的概念,包含的属性有“帐号”、“密 码”、“存款余额”、“利率”、“最小余额”,定义封装这些 属性的方法。账号要自动生成。 编写主类,使用银行账户类,输入、输出3个储户的上述信息。 考虑:哪些属性可以设计成static属性。 */
package com.jerry.exer; import java.util.Random; /** * @author jerry_jy * @create 2022-09-30 9:51 */ public class Exer1 { public static void main(String[] args) { System.out.println("最小余额:" + Account.minBalance); System.out.println("年利率:" + Account.rate); Account account = new Account("1001", 1001); System.out.println(account.toString()); Account account1 = new Account("1002", 1002); System.out.println(account1.toString()); } } class Account { private int id; private String pwd; private double balance; static double rate=0.001; static double minBalance=1; private static int init = 1001; public Account() { id = init++; } public Account(String pwd, double balance) { id = init++; this.pwd = pwd; this.balance = balance; } public int getId() { return id; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } @Override public String toString() { return "Account{" + "id=" + id + ", pwd='" + pwd + '\'' + ", balance=" + balance + '}'; } }
题目12
单例模式--饿汉式
package com.jerry.exer; /** * @author jerry_jy * @create 2022-09-30 10:15 */ public class Exer2 { public static void main(String[] args) { Singleton singleton = Singleton.getInstance(); } } class Singleton { // 1.私有化构造器 private Singleton() { } // 2.内部提供一个当前类的实例 // 4.此实例也必须静态化 private static Singleton single = new Singleton(); // 3.提供公共的静态的方法,返回当前类的对象 public static Singleton getInstance() { return single; } }
题目13
/* 编写一个Employee类,声明为抽象类, 包含如下三个属性:name,id,salary。 提供必要的构造器和抽象方法:work()。 对于Manager类来说,他既是员工,还具有奖金(bonus)的属性。 请使用继承的思想,设计CommonEmployee类和Manager类,要求类 中提供必要的方法进行属性访问。 */
package com.jerry.exer; /** * @author jerry_jy * @create 2022-09-30 10:48 */ public class Exer4 { public static void main(String[] args) { Manager manager = new Manager("jerry", 1001, 1000, 2000); manager.work(); } } abstract class Employee { String name; int id; double salary; public Employee() { } public Employee(String name, int id, double salary) { this.name = name; this.id = id; this.salary = salary; } public abstract void work(); } class Manager extends Employee { double bonus; public Manager() { } public Manager(String name, int id, double salary, double bonus) { super(name, id, salary); this.bonus = bonus; } @Override public void work() { System.out.println("working...."); } }
题目14
/* 编写工资系统,实现不同类型员工(多态)的按月发放工资。如果当月出现某个 Employee对象的生日,则将该雇员的工资增加100元。 实验说明: (1)定义一个Employee类,该类包含: private成员变量name,number,birthday,其中birthday 为MyDate类的对象; abstract方法earnings(); toString()方法输出对象的name,number和birthday。 (2)MyDate类包含: private成员变量year,month,day ; toDateString()方法返回日期对应的字符串:xxxx年xx月xx日 (3)定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处 理。该类包括:private成员变量monthlySalary; 实现父类的抽象方法earnings(),该方法返回monthlySalary值;toString()方法输 出员工类型信息及员工的name,number,birthday。 */
package com.jerry.exer; import java.util.Date; /** * @author jerry_jy * @create 2022-09-30 11:17 */ public class Exer5 { public static void main(String[] args) { MyDate date = new MyDate(2022, 9, 01); SalariedEmployee employee = new SalariedEmployee("jerry", 1001,date , 2000); employee.earnings(); if (new Date().getMonth()+1==date.getMonth()){ System.out.println("本月是你的生日,本月薪水:"+employee.getMonthlySalary()+100); }else { System.out.println("本月不是你的生日,本月薪水:"+employee.getMonthlySalary()); } System.out.println(employee.toString()); } } abstract class Employee1 { private String name; private int number; private MyDate birthday; public Employee1() { } public Employee1(String name, int number, MyDate birthday) { this.name = name; this.number = number; this.birthday = birthday; } abstract void earnings(); @Override public String toString() { return "Employee1{" + "name='" + name + '\'' + ", number=" + number + ", birthday=" + birthday.toDateString() + '}'; } } class MyDate { private int year, month, day; public MyDate() { } public MyDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public String toDateString() { String str = this.getYear() + "年" + this.getMonth() + "月" + this.getDay() + "日"; return str; } } class SalariedEmployee extends Employee1 { private double monthlySalary; public SalariedEmployee() { } public double getMonthlySalary() { return monthlySalary; } public void setMonthlySalary(double monthlySalary) { this.monthlySalary = monthlySalary; } public SalariedEmployee(String name, int number, MyDate birthday, double monthlySalary) { super(name, number, birthday); this.monthlySalary = monthlySalary; } @Override void earnings() { // if (new Date().getMonth()== salariedEmployee.) System.out.println("本月工资:" + monthlySalary); } }
题目15
/* 参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的 员工处理。该类包括: private成员变量wage和hour; 实现父类的抽象方法earnings(),该方法返回wage*hour值; toString()方法输出员工类型信息及员工的name,number,birthday。 */
package com.jerry.exer; /** * @author jerry_jy * @create 2022-09-30 15:31 */ public class Exer6 { public static void main(String[] args) { MyDate1 birth = new MyDate1(2022, 9, 30); HourlyEmployee hourlyEmployee = new HourlyEmployee("jerry", 1001, birth, 8, 60); hourlyEmployee.earnings(); System.out.println(hourlyEmployee.toString()); } } abstract class Employee2 { private String name; private int number; private MyDate1 birthday; public Employee2() { } public Employee2(String name, int number, MyDate1 birthday) { this.name = name; this.number = number; this.birthday = birthday; } abstract void earnings(); @Override public String toString() { return "Employee1{" + "name='" + name + '\'' + ", number=" + number + ", birthday=" + birthday.toDateString() + '}'; } } class MyDate1 { private int year, month, day; public MyDate1() { } public MyDate1(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public String toDateString() { String str = this.getYear() + "年" + this.getMonth() + "月" + this.getDay() + "日"; return str; } } class HourlyEmployee extends Employee2 { private int hour; private double wage; public HourlyEmployee() { } public HourlyEmployee(String name, int number, MyDate1 birthday, int hour, double wage) { super(name, number, birthday); this.hour = hour; this.wage = wage; } @Override void earnings() { System.out.println("工资:" + wage * hour); } }
–end–