class Circle{
// private final double PI = 3.14;//直接赋值
private final double PI;
private double radius;
{
PI = 3.14; // 代码块赋值
}
public Circle(double radius) {
super();
this.radius = radius;
// PI = 3.14; //构造器赋值
}
public double s(){
return PI * radius * radius;
}
}
public class Test {
public static void main(String[] args) {
System.out.println(new Circle(1.0).s());
}
}
4.代码
abstract class Employee{
private String name;
private String id;
private int salary;
public Employee(String name, String id, int salary) {
super();
this.name = name;
this.id = id;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public abstract void work();//抽象方法不能有方法体
}
class Manager extends Employee{
private int bonus;
public Manager(String name, String id, int salary, int bonus) {
super(name, id, salary);
this.bonus = bonus;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
@Override
public void work() {
System.out.println("普通员工:" + ",姓名: "+ getName() + ",id:" + getId() + ",薪水:"+ getSalary() + ",奖金:" + bonus +",工作中");
}
}
class CommonEmployee extends Employee{
public CommonEmployee(String name, String id, int salary) {
super(name, id, salary);
}
@Override
public void work() {
System.out.println("普通员工:" + ",姓名: "+ getName() + ",id:" + getId() + ",薪水:"+ getSalary() +",工作中");
}
}
public class Test {
public static void main(String[] args) {
new CommonEmployee("ada", "123", 100).work();
new Manager("jack", "001", 200, 1000).work();
}
}
public class A02 {
public static void main(String[] args) {
Week[] weeks = Week.values();//返回当前枚举类中的所有常量(枚举对象,即数组)
System.out.println("========所有星期的信息如下======");
//使用增强数组
for(Week week: weeks){
System.out.println(week);
}
}
}
enum Week{
MONDAY("星期一"),TUESDAY("星期二"),WEDNESDAY("星期三"),THURSDAY("星期四"),
FRIDAY("星期五"),SATURDAY("星期六"),SUNDAY("星期日");
private String name;
public String getName() {
return name;
}
private Week(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
又错了……
静态变量只随着类的加载而执行一次
public class Frock {
private static int cunnentNum = 100000;//衣服出场序列号起始值
private int serialNumber;//(序列号)属性
public static int getCunnentNum() {
return cunnentNum;
}
public static void setCunnentNum(int cunnentNum) {
Frock.cunnentNum = cunnentNum;
}
public int getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(int serialNumber) {
this.serialNumber = serialNumber;
}
public Frock() {
super();
serialNumber = getNextNum();
}
public static int getNextNum(){
//生成上衣唯一序列号的方法
cunnentNum += 100;
return cunnentNum;
}
}
public class TestFrock {
public static void main(String[] args) {
//静态方法可以通过类名.方法名调用
System.out.println(Frock.getNextNum());
System.out.println(Frock.getNextNum());
//获得序列号
System.out.println(new Frock().getSerialNumber());
System.out.println(new Frock().getSerialNumber());
System.out.println(new Frock().getSerialNumber());
}
}
public class A03 {
public static void main(String[] args) {
Animal cat = new Cat(); //父类的引用指向了子类的对象(多态) 向上转型
Animal dog = new Dog();
cat.shout();
dog.shout();
}
}
abstract class Animal{
abstract void shout();
}
class Cat extends Animal{
@Override
void shout() {
System.out.println("猫会喵喵叫");
}
}
class Dog extends Animal{
@Override
void shout() {
System.out.println("狗会汪汪叫");
}
}
interface Cal{
void work();
}
class Cellphone implements Cal{
//当调用该方法时,直接传递一个实现了Cal接口的 匿名内部类 即可
public void testWork(Cal cal){
//
System.out.println("Cellphone的testWork");
}
@Override
public void work() {
}
}
public class A01 {
public static void main(String[] args) {
Cellphone cellphone = new Cellphone();
cellphone.testWork(new Cal(){
//匿名内部类 当作实参传入方法中
/** * 匿名内部类: new 类/接口(参数列表){类体}; */
@Override
public void work() {
System.out.println("调用了CellPhone对象的testWork方法");
}
});
}
}
说实话,这里对我目前来说还是有点难理解……往后加强练习吧
public class Homework {
public static void main(String[] args) {
Cellphone cellphone = new Cellphone();
cellphone.testwork(new ICalculate() {
@Override
public double work(double n1, double n2) {
return n1 + n2;
}
}, 8, 10);
cellphone.testwork(new ICalculate() {
@Override
public double work(double n1, double n2) {
return n1 * n2;
}
}, 8, 10);
}
interface ICalculate{
//仅提供方法,不提供具体实现(交给匿名内部类来完成)
public double work(double n1, double n2);
}
class Cellphone{
public void testwork(ICalculate iCalculate, double n1, double n2){
double result = iCalculate.work(n1, n2);
System.out.println("计算后的结果为:" + result);
}
}
}
public class A01 {
public static void main(String[] args) {
new A().f1();//调用包含局部内部类的方法即可
}
}
class A{
private String name = "外部类A的名字";//私有变量
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void f1(){
class B{
//局部内部类
private static final String name = "内部类B的名字";
public void show(){
System.out.println( A.this.name);
System.out.println(name);
}
}
B b = new B();//别忘了创建对象
b.show();
}
}
- 慢慢体会……
public class A02 {
//这里的A02就是交通工具工厂类
private static Horse horse = new Horse();//【饿汉式】
private A02(){
//构造器私有化
}
//两个方法分别获得交通工具Horse和Boat
public static Horse gethorse(){
// return new Horse(); //马一直是白龙马【饿汉式 】
return horse;
}
public static Boat getboat(){
return new Boat(); //船可以是新船
}
public static Air getair(){
return new Air(); //飞机可以是新飞机
}
public static void main(String[] args) {
Person person = new Person("唐僧", new Horse());
person.common();
person.passRiver();
person.passRiver();
person.common();
person.mountain();
}
}
interface Vehicles{
void work();
}
class Horse implements Vehicles{
@Override
public void work() {
System.out.println("一般情况下骑马");
}
}
class Boat implements Vehicles{
@Override
public void work() {
System.out.println("遇到大河时坐船");
}
}
class Air implements Vehicles{
@Override
public void work() {
System.out.println("过火焰山坐飞机");
}
}
class Person{
private String name;
private Vehicles vehicles;
//在创建人对象时,事先给他分配一个交通工具
public Person(String name, experiments.Vehicles vehicles) {
super();
this.name = name;
this.vehicles = vehicles;
}
//编程思想:把具体要求封装成方法【一般情况下,过河情况下】
public void passRiver(){
//得到船
//判断一下,当前的 vehicles 属性是空,就获取一条船
//因为默认传入的是马,所以会一直使用马 ===> instanceof
// if(vehicles == null){
if(!(vehicles instanceof Boat)){
//当前不是船或空,就获取船
//向上转型(体现多态)
vehicles = A02.getboat();
}
//使用接口调用
vehicles.work();
}
public void common(){
//得到马
//判断一下,当前的 vehicles 属性是空,就获取一匹马
// if(vehicles == null){
if(!(vehicles instanceof Horse)){
//当前不是马或空,就获取马
//向上转型(体现多态)
vehicles = A02.gethorse();
}
//使用接口调用
vehicles.work();
}
public void mountain(){
//得到马
//判断一下,当前的 vehicles 属性是空,就获取一架飞机
// if(vehicles == null){
if(!(vehicles instanceof Air)){
//当前不是飞机或空,就获取飞机
//向上转型(体现多态)
vehicles = A02.getair();
}
//使用接口调用
vehicles.work();
}
}
public class A03 {
public static void main(String[] args) {
// Car c1 = new Car();
// Car c2 = new Car();
// Car c3 = new Car();
// c1.a.flow(41);
// c2.a.flow(-10);
// c3.a.flow(28);
Car c1 = new Car(41);
Car c2 = new Car(28);
Car c3 = new Car(-10);
c1.getAir().flow();
c2.getAir().flow();
c3.getAir().flow();
}
}
class Car{
//车类
private double temperature;
public Car(double temperature) {
super();
this.temperature = temperature;
}
public double getTemperature() {
return temperature;
}
public void setTemperature(double temperature) {
this.temperature = temperature;
}
class Air{
//空调类
public void flow(){
//吹风功能
if(temperature > 40){
System.out.println("温度为:" + temperature + ",吹冷风");
}else if(temperature < 0 ){
System.out.println("温度为:" + temperature + ",吹暖气");
}else{
System.out.println("温度为:" + temperature + ",关闭空调");
}
}
}
// Air a = new Air();
//创建一个返回Air对象的方法
public Air getAir(){
return new Air();
}
}
public class A05 {
public static void main(String[] args) {
//枚举值的switch使用
Color red = Color.RED;
red.show();
//switch()中,放入枚举对象
switch(red){
case RED:
System.out.println("匹配到红色");
break;
case BLUE:
System.out.println("匹配到蓝色");
break;
case BLACK:
System.out.println("匹配到黑色");
break;
case YELLOW:
System.out.println("匹配到黄色");
break;
case GREEN:
System.out.println("匹配到绿色");
break;
default:
System.out.println("没有匹配到……");
}
}
}
interface ShowColor{
public void show();
}
enum Color implements ShowColor{
RED(255,0,0),BLUE(0,0,255),BLACK(0,0,0),YELLOW(255,255,0),GREEN(0,255,0);
private int redValue;
private int greenValue;
private int blueValue;
private Color(int redValue, int greenValue, int blueValue) {
this.redValue = redValue;
this.greenValue = greenValue;
this.blueValue = blueValue;
}
public int getRedValue() {
return redValue;
}
public int getGreenValue() {
return greenValue;
}
public int getBlueValue() {
return blueValue;
}
@Override
public void show() {
System.out.println("属性值为:"+ redValue + ","+ greenValue + ","+blueValue);
}
}