public static void main(String []args){
Car c = new Car();
Car c1 = new Car(100);
System.out.println(c);
System.out.println(c1);
}
class Car{
double price = 10;
static String color = "white";
public String toString(){
return price+"\t"+color;
}
public Car(){
this.price = 9;
this.color = "red";
}
public Car(double price){
this.price = price;
}
}
步骤分析:
创建对象调用无参构造器,将此对象的price 改成9,将静态变量color 改成red 。将此对象地址给到 c
创建另一个对象调用有参构造器,将此对象的price 改成100,将此对象地址给到 c1
输出这两个对象,自动调用Car类中重写的toString方法输出:
9 red
100 red
public class TestFrock {
public static void main(String[] args) {
Frock f1 = new Frock();
Frock f2 = new Frock();
Frock f3 = new Frock();
System.out.println("f1的序列号为:"+f1.getSeriaNumber()+"f2的序列号为:"
+f2.getSeriaNumber()+"f3的序列号为:"+f3.getSeriaNumber());
}
}
class Frock{
private static int currentNum = 100000;
private int seriaNumber;
public Frock() {
this.seriaNumber = getNextNumber();
}
public static int getNextNumber() {
return currentNum+=100;
}
public int getSeriaNumber() {
return seriaNumber;
}
}
运行结果:
f1的序列号为:100100 f2的序列号为:100200 f3的序列号为:100300
public class TestAnimal {
public static void main(String[] args) {
new Cat().shou();
new Dog().shou();
}
}
abstract class Animal{
public abstract void shou();
}
class Cat extends Animal{
@Override
public void shou() {
System.out.println("小猫喵喵叫");
}
}
class Dog extends Animal{
@Override
public void shou() {
System.out.println("小狗汪汪叫");
}
}
public class testInterface {
public static void main(String[] args) {
new Cellphone().testWork(new Calculator() {//匿名内部类
@Override
public double work(double a, double b) {
return a+b;
}
},89,102);
}
}
interface Calculator{
public abstract double work(double a,double b);
}
class Cellphone {
public double testWork(Calculator calculator,double n1,double n2){
double sum = calculator.work(n1,n2);
System.out.println(sum);
return sum;
}
}
public class Test3 {
public static void main(String[] args) {
new A().newB();
}
}
class A{
private String name = "A的名字";
public void newB(){
class B{
private String name = "B的名字";
public void show(){
System.out.println(name+A.this.name);
}
}
new B().show();
}
}
public class test3 {//测试运行类
public static void main(String[] args) {
Person tang = new Person("唐僧",Factory.getHorse());
tang.normal();
tang.river();
tang.normal();
tang.normal();
}
}
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 Factory{//交通工具工厂类
public static Horse getHorse(){//获取马的方法
return new Horse();
}
public static Boat getBoat(){//获取船的方法
return new Boat();
}
}
class Person{
private String name;
private Vehicles vehicles;
public Person(String name, Vehicles vehicles) {
this.name = name;
this.vehicles = vehicles;
}
public void normal(){//正常情况
if (vehicles instanceof Horse){
vehicles.work();
}else{
vehicles = Factory.getHorse();
vehicles.work();
}
}
public void river(){//遇到河了
if (vehicles instanceof Boat){
vehicles.work();
}else{
vehicles = Factory.getBoat();
vehicles.work();
}
}
}
这里直接已实现效果为目的了,如果要进行优化,符合实际逻辑。可以将马设计成枚举类对象或者使用单例设计模式,毕竟唐僧不是一遇到河就把马龙马丢掉的。
public class test4 {
public static void main(String[] args) {
//外部其他类调用成员内部类的方式1,外部类对象.成员内部类对象.方法名
System.out.println(new Car(-12.3).new Air().flow());
System.out.println(new Car(38).new Air().flow());
System.out.println(new Car(45).new Air().flow());
}
}
class Car{
double temperature;
public Car(double temperature) {
this.temperature = temperature;
}
class Air{
public String flow(){
if (temperature < 0){
return "怕你被冻死,给你吹暖气";
}else if(temperature >=0 && temperature <=40){
return "温度正常,空调已关闭";
}else{
return "怕你被热死,给你吹冷气";
}
}
}
}
public class test5 {
public static void main(String[] args) {
Color color = Color.YELLOW;
switch (color){
case RED:
color.show();
break;
case BLUE:
color.show();
break;
case GREEN:
color.show();
break;
case BALCK:
color.show();
break;
case YELLOW:
color.show();
break;
default:
System.out.println("为查找到");
}
}
}
enum Color implements inMy{
RED(255,0,0),BLUE(0,0,255),
BALCK(0,0,0),YELLOW(255,255,0),
GREEN(0,255,0);
private int redValue;
private int greenValue;
private int blueValue;
Color(int redValue, int greenValue, int blueValue) {
this.redValue = redValue;
this.greenValue = greenValue;
this.blueValue = blueValue;
}
@Override
public void show() {
System.out.println("属性值为:"+redValue+","+greenValue+","+blueValue);
}
}
interface inMy{
void show();
}