微信上优惠券的网站怎么做的,软件公司需要什么资质,石家庄网络科技有限公司,设计师网站官网目录 一、static关键字
1.1 静态变量
1.2 静态内存解析
1.3 static的应用与练习
二、单例设计模式
2.1 单例模式
2.2 如何实现单例模式
三、代码块
3.1 详解
3.2 练习#xff0c;测试
四、final关键字
五、抽象类与抽象方法
5.1 abstract
5.2 练习
六、接口
6.…目录 一、static关键字
1.1 静态变量
1.2 静态内存解析
1.3 static的应用与练习
二、单例设计模式
2.1 单例模式
2.2 如何实现单例模式
三、代码块
3.1 详解
3.2 练习测试
四、final关键字
五、抽象类与抽象方法
5.1 abstract
5.2 练习
六、接口
6.1 接口的理解
6.2 接口的多态性
6.3 抽象类和接口对比
6.4 接口的使用练习
6.4.1 练习1
1. 定义接口
2. 实现接口
3. 使用接口
4. 测试支付系统
5.结果
6.4.2 练习2
1. 接口
2. Developer类
3. 父类Vehicle
4. 三个子类 Bicycle ElectricVhicle Car
5. VehicleTest测试类
6. 结果 一、static关键字
1.1 静态变量
static静态的用来修饰的结构、属性、方法代码块、内部类
对比静态变量与实例变量:
①个数
静态变量:在内存空间中只有一份 被类的多个对象所共享。实例变量:类的每一个实例(或对象)都保存着一份实例变量。
②内存位置
静态变量: jdk6及之前:存放在方法区。jdk7及之后: 存放在堆空间实例变量:存放在堆空间的对象实体中。
③加载时机
静态变量:随着类的加载而加载由于类只会加载一次 所以静态变量也只有一份。实例变量:随着对象的创建而加载。每个对象拥有一份实例变量。
④调用者
静态变量:可以被类直接调用也可以使用对象调用。实例变量:只能使用对象进行调用。
⑤判断是否可以调用--- 从生命周期的角度解释
类变量实例变量类yesno对象yesyes
⑥消亡时机.
静态变量:随着类的卸载而消亡实例变量:随着对象的消亡而消亡
1.2 静态内存解析 1.3 static的应用与练习
import java.util.Objects;public class Account {private int id;private String password;//密码private double balance;//金额private static double interestRate;//利率private static double minBalance 1.0;private static int init 1001;public Account(){this.id init;init;password 000000;}public Account(String password, double balance) {this.password password;this.balance balance;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}public double getBalance() {return balance;}public void setBalance(double balance) {this.balance balance;}public static double getInterestRate() {return interestRate;}public static void setInterestRate(double interestRate) {Account.interestRate interestRate;}public static double getMinBalance() {return minBalance;}public static void setMinBalance(double minBalance) {Account.minBalance minBalance;}Overridepublic String toString() {return Account{ id id , password password \ , balance balance };}
}public class Test {public static void main(String[] args) {Account acct1 new Account();System.out.println(acct1);Account acct2 new Account(123456,1155);System.out.println(acct2);Account.setInterestRate(0.0123);Account.setMinBalance(10);System.out.println(银行存款的利率 Account.getInterestRate());System.out.println(银行最小存款: Account.getMinBalance());}
}
二、单例设计模式
2.1 单例模式
何为单例模式
采取一定的办法保证在整个软件系统中堆某个类只能存在一个对象实例并且该类只提供一个取得其对象实例的方法。
经典设计模式共23种 2.2 如何实现单例模式
1.饿汉式:
public class boyFirend {private int age;private String name;//1.私有化构造器private boyFirend(){}//2.创建对象私有化private static boyFirend b1 new boyFirend();//3.public static boyFirend getG1(){return b1;}
}
2.懒汉式
//懒汉式
public class GirlFirend {private int age;private String name;//1.私有化构造器private GirlFirend(){}//2.创建对象私有化private static GirlFirend g1 null;//3.public static GirlFirend getG1(){if (g1 null) {g1 new GirlFirend();}return g1;}
}两种模式的对比
特点
饿汉式:立即加载随着类的加载当前唯一的实例创建。懒汉式:延迟加载在需要使用时进行创建
优缺点
饿汉式优点写法简单使用更快线程安全缺点内存中占用时间长。懒汉式优点节省内存空间缺点线程不安全
三、代码块
3.1 详解
用来初始化类或对象的信息即初始化类或对象的成员变量
代码块修饰只能使用static进行修饰
代码块分类
静态代码块;使用static修饰非静态代码块不适用static修饰
格式 { //内容 } static{ //内容 } 3.2 练习测试
class User {private String userName;private String password;private long registrationTime;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}public long getRegistrationTime() {return registrationTime;}{System.out.println(新用户注册);registrationTime System.currentTimeMillis();}public User() {userName System.currentTimeMillis() ;password 123456;}public User(String userName, String password) {this.userName userName;this.password password;}public String getInfo() {return用户名 userName , 密码 password , 注册时间 registrationTime ;}
}
public class Test {public static void main(String[] args) {User u1 new User();System.out.println(u1.getInfo());User u2 new User(张三,654321);System.out.println(u2.getInfo());}
}
注意运行上由父及子静态先行。记住执行的先后顺序默认-显式-代码块-构造器-对象
小测试请给出运行结果
public class Test {public static void main(String[] args) {Sub s new Sub();}
}class Base{Base(){method(100);}{System.out.println(base);}public void method(int i){System.out.println(base : i);}
}class Sub extends Base{Sub(){super.method(70);}{System.out.println(Sub);}public void method(int j){System.out.println(sub : j);}
}
答案 四、final关键字
final关键字: 修饰符用于限制类、方法和变量的行为。
可在哪些位置赋值
显示代码块中构造器中
final作用
修饰类表此类不能被继承
final class FinalClass {void display() {System.out.println(This is a final class.);}
}// class SubClass extends FinalClass { // 这行代码会导致编译错误
// }修饰方法此方法不能被重写
class Parent {final void show() {System.out.println(This is a final method.);}
}class Child extends Parent {// void show() { // 这行代码会导致编译错误// System.out.println(Trying to override a final method.);// }
}修饰变量成员和局部变量都可修饰此时“变量”其实变成了“常量”不可更改。
final int x 10;
// x 20; // 这行代码会导致编译错误final StringBuilder a1 new StringBuilder(Hello);
// a1 new StringBuilder(World); // 这行代码会导致编译错误
a1.append(, World!); // 这行是允许的因为a1引用的对象内容是可以改变的final搭配static使用等于全局常量
class Constants {static final int MAX_USERS 100;static final String APP_NAME MyApp;
}习题练习 报错x导致x的值改变
五、抽象类与抽象方法
5.1 abstract
abstract抽象的
abstract修饰类
此类为抽象类。抽象类不能实例化。抽象类中包含构造器因为子类实例化直接或间接调用父类的构造器。抽象类可无抽象方法有抽象方法一定为抽象类。
abstract修饰方法
此方法为抽象方法。抽象方法只有方法声明没有方法体。抽象方法的功能确定不知具体实现。抽象方法必须重写父类中的所有抽象方法才能实例化否则此子类还是抽象类。
abstract不能使用的场景属性构造器代码块
5.2 练习
场景编写工资系统不同类型的员工多态按月发工资如果某个月是某个employee对象的生日则当月的工资增加100 代码:
//Employee类
public abstract class Employee {private String name;private int number;private MyDate birthday;public Employee() {}public Employee(String name, int number, MyDate birthday) {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();Overridepublic String toString() {return Employee{ name name \ , number number , birthday birthday.toDateString() \ };}
}//HourEmployee类
public class HourEmployee extends Employee{private double wage;private int hour;public HourEmployee() {}public HourEmployee(String name, int number, MyDate birthday, double wage, int hour) {super(name, number, birthday);this.wage wage;this.hour hour;}public double getWage() {return wage;}public void setWage(double wage) {this.wage wage;}public int getHour() {return hour;}public void setHour(int hour) {this.hour hour;}Overridepublic double earnings() {return wage * hour;}Overridepublic String toString() {return HourEmployee{ super.toString() };}
}//MyDate类
public class MyDate {private int year;private int month;private int 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(){return year 年 month 月 day 日;}
}//PayrollSystem类
import java.util.Scanner;public class PayrollSystem {public static void main(String[] args) {Scanner scan new Scanner(System.in);Employee[] emps new Employee[2];emps[0] new SalariedEmployee(张胜男, 1001,new MyDate(1992, 12, 30), 2000);emps[1] new HourEmployee(李四, 1002,new MyDate(1992, 10, 10),15 ,240);System.out.println(请输入当前的月份 );int month scan.nextInt();for (int i 0; i emps.length; i) {System.out.println(emps[i].toString());if (month emps[i].getBirthday().getMonth()){double a 100.0;double b emps[i].earnings() a;System.out.println(工资为 b);System.out.println(生日快乐加薪100);}else{System.out.println(工资为emps[i].earnings());}scan.close();}}
}//SalariedEmployee类
public class SalariedEmployee extends Employee{private double monthlySalary;public SalariedEmployee() {}Overridepublic double earnings(){return monthlySalary;}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;}Overridepublic String toString() {return SalariedEmployee{ super.toString() };}
}
六、接口
6.1 接口的理解
1. 接口的本质是契约、标准、规范就像我们的法律一样。制定好后大家都要遵守。
2.定义接口的关键字: interface
3.接口内部结构的说明:
可以声明:
属性:必须使用public static final修饰方法: jdk8之前:声明抽象方法修饰为public abstractjdk8 :声明静态方法、默认方法jdk9 :声明私有方法
不可以声明:构造器
4.接口与类的关系:实现关系
5.格式:
class A extends SuperA implements B C{}A相较于SuperA来讲叫做子类A相较于B, C来讲叫做实现类。
6.满足此关系之后说明:
类可以实现多个接口。类针对于接口的多实现一定程度上就弥补了类的单继承的局限性。类必须将实现的接口中的所有的抽象方法都重写(或实现)方可实例化。否则此实现类必须声明为抽象类。
7.接口与接口的关系:继承关系且可以多继承
6.2 接口的多态性
接口名 变量名 new 实现类对象;
以下为举例Computer是类transferData()方法USB是接口printer打印机
创建接口实现类的对象 Computer computer new Computer(); Printer printer new printer(); 创建接口实现类的匿名对象 computer.transferData (new Computer); 创建接口匿名实现类的对象 USB usb1 new USB(){ //重写接口中方法 } computer.transferData (usb1); 创建接口匿名实现类的匿名对象 computer.transferData (new USB(){ //重写接口中方法 }); 6.3 抽象类和接口对比 6.4 接口的使用练习
6.4.1 练习1
假设要创建一个不同的支付方式如信用卡、支付宝需要实现相同的接口。
1. 定义接口
定义一个支付接口 Payment包括方法 pay() 和 refund()。
public interface Payment {void pay(double amount);void refund(double amount);
}2. 实现接口
实现 CreditCardPayment 和 AliPay 两种支付方式。
public class CreditCardPayment implements Payment {Overridepublic void pay(double amount) {System.out.println(使用信用卡支付 amount 元);}Overridepublic void refund(double amount) {System.out.println(退款到信用卡 amount 元);}
}public class AliPay implements Payment {Overridepublic void pay(double amount) {System.out.println(使用支付宝支付 amount 元);}Overridepublic void refund(double amount) {System.out.println(退款到支付宝 amount 元);}
}3. 使用接口
创建一个简单的支付处理类使用接口来处理支付逻辑。
public class PaymentProcessor {private Payment payment;public PaymentProcessor(Payment payment) {this.payment payment;}public void processPayment(double amount) {payment.pay(amount);}public void processRefund(double amount) {payment.refund(amount);}
}4. 测试支付系统
在主程序中创建不同的支付方式并测试。
public class Main {public static void main(String[] args) {// 使用信用卡支付Payment creditCardPayment new CreditCardPayment();PaymentProcessor creditCardProcessor new PaymentProcessor(creditCardPayment);creditCardProcessor.processPayment(100.0);creditCardProcessor.processRefund(50.0);// 使用支付宝支付Payment aliPayPayment new AliPay();PaymentProcessor aliPayProcessor new PaymentProcessor(aliPayPayment);aliPayProcessor.processPayment(200.0);aliPayProcessor.processRefund(80.0);}
}5.结果 6.4.2 练习2
模拟场景 UML类图UML类图讲解可跳转 构造器和UML类图_类图中怎么创建加号-CSDN博客) 代码实现
1. 接口
interface IPower {public void power();
}
2. Developer类
package test2;public class Developer {private int age;public String name;public int getAge() {return age;}public void setAge(int age) {this.age age;}public String getName() {return name;}public void setName(String name) {this.name name;}public void takingVehicle(Vehicle vehicle){vehicle.run();}
}3. 父类Vehicle
package test2;public abstract class Vehicle {private String brand;private String color;public Vehicle() {}public Vehicle(String brand, String color) {this.brand brand;this.color color;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand brand;}public String getColor() {return color;}public void setColor(String color) {this.color color;}public abstract void run();
}4. 三个子类
Bicycle-ElectricVhicle-Car
package test2;public class Car extends Vehicle implements IPower{private String carName;public Car() {}public Car(String brand, String color, String carName) {super(brand, color);this.carName carName;}public String getCarName() {return carName;}public void setCarName(String carName) {this.carName carName;}Overridepublic void run() {System.out.println(汽车靠内燃机驱动);}Overridepublic void power() {System.out.println(动力来自汽油);}
}package test2;public class ElectricVhicle extends Vehicle implements IPower{public ElectricVhicle() {}public ElectricVhicle(String brand, String color) {super(brand, color);}Overridepublic void run() {System.out.println(电动车电机驱动);}Overridepublic void power() {System.out.println(动力来自电力);}
}package test2;public class Bicycle extends Vehicle{public Bicycle() {}public Bicycle(String brand, String color) {super(brand, color);}Overridepublic void run() {System.out.println(靠脚蹬骑自行车);}
}
5. VehicleTest测试类
package test2;public class VehicleTest {public static void main(String[] args) {Developer developer new Developer();Vehicle[] vehicles new Vehicle[3];vehicles[0] new Bicycle(凤凰牌,黄色);vehicles[1] new ElectricVhicle(理想,蓝色);vehicles[2] new Car(奔驰,黑色,京A88888);for (int i 0; i vehicles.length; i) {developer.takingVehicle(vehicles[i]);if (vehicles[i] instanceof IPower) {((IPower) vehicles[i]).power();}System.out.println();}}
}6. 结果
文章转载自: http://www.morning.qsyyp.cn.gov.cn.qsyyp.cn http://www.morning.kclkb.cn.gov.cn.kclkb.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.rtspr.cn.gov.cn.rtspr.cn http://www.morning.qbgff.cn.gov.cn.qbgff.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.bsjpd.cn.gov.cn.bsjpd.cn http://www.morning.mxcgf.cn.gov.cn.mxcgf.cn http://www.morning.ylzdx.cn.gov.cn.ylzdx.cn http://www.morning.txmkx.cn.gov.cn.txmkx.cn http://www.morning.eviap.com.gov.cn.eviap.com http://www.morning.ljbm.cn.gov.cn.ljbm.cn http://www.morning.jfgmx.cn.gov.cn.jfgmx.cn http://www.morning.twhgn.cn.gov.cn.twhgn.cn http://www.morning.fxwkl.cn.gov.cn.fxwkl.cn http://www.morning.rqxtb.cn.gov.cn.rqxtb.cn http://www.morning.sfnjr.cn.gov.cn.sfnjr.cn http://www.morning.pwlxy.cn.gov.cn.pwlxy.cn http://www.morning.tdcql.cn.gov.cn.tdcql.cn http://www.morning.mqbzk.cn.gov.cn.mqbzk.cn http://www.morning.ctqbc.cn.gov.cn.ctqbc.cn http://www.morning.dqwykj.com.gov.cn.dqwykj.com http://www.morning.bpmfl.cn.gov.cn.bpmfl.cn http://www.morning.wdpt.cn.gov.cn.wdpt.cn http://www.morning.cmcjp.cn.gov.cn.cmcjp.cn http://www.morning.mqss.cn.gov.cn.mqss.cn http://www.morning.kjlhb.cn.gov.cn.kjlhb.cn http://www.morning.kkwbw.cn.gov.cn.kkwbw.cn http://www.morning.rszyf.cn.gov.cn.rszyf.cn http://www.morning.tnyanzou.com.gov.cn.tnyanzou.com http://www.morning.fmswb.cn.gov.cn.fmswb.cn http://www.morning.czrcf.cn.gov.cn.czrcf.cn http://www.morning.pmbcr.cn.gov.cn.pmbcr.cn http://www.morning.bpncd.cn.gov.cn.bpncd.cn http://www.morning.ndmbd.cn.gov.cn.ndmbd.cn http://www.morning.hytqt.cn.gov.cn.hytqt.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn http://www.morning.slnz.cn.gov.cn.slnz.cn http://www.morning.ksqyj.cn.gov.cn.ksqyj.cn http://www.morning.sjsfw.cn.gov.cn.sjsfw.cn http://www.morning.wjdgx.cn.gov.cn.wjdgx.cn http://www.morning.cnwpb.cn.gov.cn.cnwpb.cn http://www.morning.jwxnr.cn.gov.cn.jwxnr.cn http://www.morning.sacxbs.cn.gov.cn.sacxbs.cn http://www.morning.qxlyf.cn.gov.cn.qxlyf.cn http://www.morning.hsjfs.cn.gov.cn.hsjfs.cn http://www.morning.ztcwp.cn.gov.cn.ztcwp.cn http://www.morning.nxnrt.cn.gov.cn.nxnrt.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.shinezoneserver.com.gov.cn.shinezoneserver.com http://www.morning.pjtnk.cn.gov.cn.pjtnk.cn http://www.morning.c7497.cn.gov.cn.c7497.cn http://www.morning.lpnpn.cn.gov.cn.lpnpn.cn http://www.morning.deupp.com.gov.cn.deupp.com http://www.morning.mnslh.cn.gov.cn.mnslh.cn http://www.morning.ptqds.cn.gov.cn.ptqds.cn http://www.morning.ggnrt.cn.gov.cn.ggnrt.cn http://www.morning.ghkgl.cn.gov.cn.ghkgl.cn http://www.morning.ymsdr.cn.gov.cn.ymsdr.cn http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn http://www.morning.mmkrd.cn.gov.cn.mmkrd.cn http://www.morning.zqmdn.cn.gov.cn.zqmdn.cn http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn http://www.morning.gfjgq.cn.gov.cn.gfjgq.cn http://www.morning.ttdbr.cn.gov.cn.ttdbr.cn http://www.morning.hgcz.cn.gov.cn.hgcz.cn http://www.morning.rwwdp.cn.gov.cn.rwwdp.cn http://www.morning.ryyjw.cn.gov.cn.ryyjw.cn http://www.morning.xxwfq.cn.gov.cn.xxwfq.cn http://www.morning.wzyfk.cn.gov.cn.wzyfk.cn http://www.morning.kqxwm.cn.gov.cn.kqxwm.cn http://www.morning.nrzbq.cn.gov.cn.nrzbq.cn http://www.morning.nrlsg.cn.gov.cn.nrlsg.cn http://www.morning.mjbnp.cn.gov.cn.mjbnp.cn http://www.morning.nchlk.cn.gov.cn.nchlk.cn http://www.morning.kjfqf.cn.gov.cn.kjfqf.cn http://www.morning.rsbqq.cn.gov.cn.rsbqq.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn