package TEXT.LIAN;
public class mianText {
public static void main(String[] args) {
String[] PP=new String[100];
Main.main(PP);
}
}
class Main{
public static void main(String[] args) {
for (int i=0;i
args[i] ="args_"+i;
System.out.println(args[i]);
}
}
}
stasic{}(静态代码块)和 {}(非静态的代码块)
package text2.test1;
public class BlockTest {
public static void main(String[] args) {
String desc = Person.desc;
System.out.println(desc);
//造对象
Person person = new Person();
Person p2 = new Person();
System.out.println(person.age);
Person.info();
}
}
class Person {
String name;
int age;
static String desc = "我是一个人";
//空参构造器
public Person() {
}
//待参构造器
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//代码块
static {
System.out.println("hello,static block");
desc="爱学校的";
}
//非静态代码块
{
System.out.println("hello block");
age=11;
}
//方法
public void eat() {
System.out.println("吃饭");
}
// toString方法
@Override
public String toString() {
return "Person[name=" + name + ",age=" + age + "]";
}
public static void info(){
System.out.println("我是个快乐的人!");
}
}
静态代码和非静态代码块的排序执行
package text2.test1;
public class LeafTest {
public static void main(String[] args) {
new Leaf();
//静态代码块只执行一次
// new Leaf();
}
}
class Root {
static {
System.out.println("Root的静态初始化块");
}
{
System.out.println("Root的普通初始化");
}
public Root() {
super();
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("尚硅谷");
System.out.println("Leaf的带参构造器");
}
}
1.静态代码和非静态代码块的排序执行
package text2.test1;
public class Son extends Father {
static {
System.out.println("4444444444444");
}
{
System.out.println("5555555555555");
}
public Son() {
System.out.println("6666666666666");
}
public static void main(String[] args){
System.out.println("7777777777777");
System.out.println("*************");
new Son();
}
}
class Father {
static {
System.out.println("11111111111111");
}
{
System.out.println("22222222222222");
}
}
抽象的使用(abstract)
package text3.abstract1;
public class AbstractTest {
public static void main(String[] args) {
//一旦Person类抽象了,就不可实例化
}
}
//抽象类
abstract class Creature {
//抽象方法
public abstract void breath();
}
//抽象类的继承
abstract class Person extends Creature {
//抽象方法
public abstract void eaopop();
//属性
String name;
int age;
//构造器
public Person() {
}
//带参构造器
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//方法
public void eat() {
System.out.println("人吃饭");
}
//方法
public void waIk() {
System.out.println("人走路");
}
}
//抽象类的继承,里面重写了抽象方法
//Studer类可以实例化对象
class Studet extends Person {
//带参构造器
public Studet(String name, int age) {
super(name, age);
}
//无参构造器
public Studet() {
}
//重写父类抽象类的方法
public void eaopop() {
}
//重写父类抽象类的方法
public void breath() {
}
}
class PersonTest {
public static void main(String[] args) {
method(new Studet());//匿名对象
Worker worker = new Worker();
method1(worker);//非匿名的类非匿名的对象
method1(new Worker());//非匿名的类匿名对象
System.out.println("***********************");
//创建了一匿名子类的对象:p
//创建实例进行赋值
Person opp= new Person() {
@Override
public void eaopop() {
System.out.println("吃你");
}
@Override
public void breath() {
System.out.println("吃你");
}
};
method1(opp);
}
public static void method1(Person p) {
p.breath();
p.eaopop();
}
public static void method(Studet s) {
}
}
class Worker extends Person {
public void breath() {
}
public void eaopop() {
}
}
工资系统
主方法
package case1;
import java.util.Calendar;
import java.util.Scanner;
public class PayrollSystem {
public static void main(String[] args) {
//方式一:
// Scanner scanner = new Scanner(System.in);
// System.out.println("请输入当月的月份:");
// int month = scanner.nextInt();
//方式二:
Calendar calendar = Calendar.getInstance();
int month = calendar.get(Calendar.MONTH);//获取当前的月份
// System.out.println(month);//一月份:0
Employee[] emps = new Employee[3];
emps[0] = new SalariedEmployee("马森", 1002,new MyDate(1992, 5, 28),10000);
emps[1] = new HourlyEmployee("潘雨生", 2001, new MyDate(1991, 1, 6),60,240);
emps[2] = new SalariedEmployee("阿萨德", 2007, new MyDate(1991, 1, 6),2940);
for(int i = 0;i < emps.length;i++){
System.out.println(emps[i]);
double salary = emps[i].earnings();
System.out.println("月工资为:" + salary);
if((month+1) == emps[i].getBirthday().getMonth()){
System.out.println("生日快乐!奖励100元");
}
}
}
}
父类
package case1;
abstract public class Employee {
private String name;
private int number;
private MyDate birthday;
public Employee(String name, int number, MyDate birthday) {
// super();
this.name = name;
this.number = number;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public abstract double earnings();
@Override
public String toString() {
return "Employee[name="+name+",number="+number+",birthday="+birthday.toDateString();
}
}
属性类
package case1;
public class MyDate {
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
// super();
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() {
return year+"年"+month+"月"+day+"日";
}
}
子类
package case1;
public class SalariedEmployee extends Employee {
private double monthlySalary;//月工资
public SalariedEmployee(String name, int number, MyDate birthday) {
super(name, number, birthday);
}
public SalariedEmployee(String name, int number, MyDate birthday, double monthlySalary) {
super(name, number, birthday);
this.monthlySalary = monthlySalary;
}
public double getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(double monthlySalary) {
this.monthlySalary = monthlySalary;
}
@Override
public double earnings() {
return monthlySalary;
}
public String toString() {
return "SalariedEmployee[" + super.toString() + "]";
}
}
子类
package case1;
public class HourlyEmployee extends Employee {
private int wage;//每小时的工资
private int hour;//月工作的小时数
public HourlyEmployee(String name, int number, MyDate birthday) {
super(name, number, birthday);
}
public HourlyEmployee(String name, int number, MyDate birthday, int wage, int hour) {
super(name, number, birthday);
this.wage = wage;
this.hour = hour;
}
public int getWage() {
return wage;
}
public void setWage(int wage) {
this.wage = wage;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
@Override
public double earnings() {
return wage * hour;
}
public String toString() {
return "HourlyEmployee[" + super.toString() + "]";
}
}



