内部类和面向对象项目实战
一、内部类
1、成员内部类
package com.lanson.test07;
/**
* 1.类的组成:属性,方法,构造器,代码块(普通块,静态块,构造块,同步块),内部类
* 2.一个类TestOuter的内部的类SubTest叫内部类, 内部类 :SubTest 外部类:TestOuter
* 3.内部类:成员内部类 (静态的,非静态的) 和 局部内部类(位置:方法内,块内,构造器内)
* 4.成员内部类:
* 里面属性,方法,构造器等
* 修饰符:private,default,protect,public,final,abstract
*/
public class TestOuter {
//非静态的成员内部类:
public class D{
int age = 20;
String name;
public void method(){
//5.内部类可以访问外部类的内容
/*System.out.println(age);
a();*/
int age = 30;
//8.内部类和外部类属性重名的时候,如何进行调用:
System.out.println(age);//30
System.out.println(this.age);//20
System.out.println(TestOuter.this.age);//10
}
}
//静态成员内部类:
static class E{
public void method(){
//6.静态内部类中只能访问外部类中被static修饰的内容
/*System.out.println(age);
a();*/
}
}
//属性:
int age = 10;
//方法:
public void a(){
System.out.println("这是a方法");
{
System.out.println("这是一个普通块");
class B{
}
}
class A{
}
//7.外部类想要访问内部类的东西,需要创建内部类的对象然后进行调用
D d = new D();
System.out.println(d.name);
d.method();
}
static{
System.out.println("这是静态块");
}
{
System.out.println("这是构造块");
}
//构造器:
public TestOuter(){
class C{
}
}
public TestOuter(int age) {
this.age = age;
}
}
class Demo{
//这是一个main方法,是程序的入口:
public static void main(String[] args) {
//创建外部类的对象:
TestOuter to = new TestOuter();
to.a();
//9.创建内部类的对象:
//静态的成员内部类创建对象:
TestOuter.E e = new TestOuter.E();
//非静态的成员内部类创建对象:
//错误:TestOuter.D d = new TestOuter.D();
TestOuter t = new TestOuter();
TestOuter.D d = t.new D();
}
}
2、局部内部类
package com.lanson.test08;
/**
* @Auther: lanson
*/
public class TestOuter {
//1.在局部内部类中访问到的变量必须是被final修饰的
public void method(){
final int num = 10;
class A{
public void a(){
//num = 20;
System.out.println(num);
}
}
}
//2.如果类B在整个项目中只使用一次,那么就没有必要单独创建一个B类,使用内部类就可以了
public Comparable method2(){
class B implements Comparable{
@Override
public int compareTo(Object o) {
return 100;
}
}
return new B();
}
public Comparable method3(){
//3.匿名内部类
return new Comparable(){
@Override
public int compareTo(Object o) {
return 200;
}
};
}
public void teat(){
Comparable com = new Comparable(){
@Override
public int compareTo(Object o) {
return 200;
}
};
System.out.println(com.compareTo("abc"));
}
}
二、面向对象项目实战
1、项目需求
2、项目结构分析
3、最终代码
匹萨父类:
package com.lanson.test01;
/**
* @Auther: lanson
* 父类:匹萨类
*/
public class Pizza {
//属性
private String name;//名称
private int size;//大小
private int price;//价格
//方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
//展示匹萨信息:
public String showPizza(){
return "匹萨的名字是:"+name+"\n匹萨的大小是:"+size+"寸\n匹萨的价格:"+price+"元";
}
//构造器
public Pizza() {
}
public Pizza(String name, int size, int price) {
this.name = name;
this.size = size;
this.price = price;
}
}
培根匹萨:
package com.lanson.test01;
/**
* @Auther: lanson
*/
public class BaconPizza extends Pizza {
//属性:
private int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
//构造器:
public BaconPizza() {
}
public BaconPizza(String name, int size, int price, int weight) {
super(name, size, price);
this.weight = weight;
}
//重写父类showPizza方法:
@Override
public String showPizza() {
return super.showPizza()+"\n培根的克数是:"+weight+"克";
}
}
水果匹萨:
package com.lanson.test01;
/**
* @Auther: lanson
*/
public class FruitsPizza extends Pizza{
//属性:
private String burdening;
public String getBurdening() {
return burdening;
}
public void setBurdening(String burdening) {
this.burdening = burdening;
}
//构造器:
public FruitsPizza() {
}
public FruitsPizza(String name, int size, int price, String burdening) {
super(name, size, price);
this.burdening = burdening;
}
//重写父类showPizza方法:
@Override
public String showPizza() {
return super.showPizza()+"\n你要加入的水果:"+burdening;
}
}
测试类:
public class Test {
//这是一个main方法,是程序的入口:
public static void main(String[] args) {
//选择购买匹萨:
Scanner sc = new Scanner(System.in);
System.out.println("请选择你想要购买的匹萨(1.培根匹萨 2.水果匹萨):");
int choice = sc.nextInt();//选择
//通过工厂获取匹萨:
Pizza pizza = PizzaStore.getPizza(choice);
System.out.println(pizza.showPizza());
}
}
工厂类:
package com.lanson.test01;
import java.util.Scanner;
/**
* @Auther: lanson
*/
public class PizzaStore {
public static Pizza getPizza(int choice){
Scanner sc = new Scanner(System.in);
Pizza p = null;
switch (choice){
case 1:
{
System.out.println("请录入培根的克数:");
int weight = sc.nextInt();
System.out.println("请录入匹萨的大小:");
int size = sc.nextInt();
System.out.println("请录入匹萨的价格:");
int price = sc.nextInt();
//将录入的信息封装为培根匹萨的对象:
BaconPizza bp = new BaconPizza("培根匹萨",size,price,weight);
p = bp;
}
break;
case 2:
{
System.out.println("请录入你想要加入的水果:");
String burdening = sc.next();
System.out.println("请录入匹萨的大小:");
int size = sc.nextInt();
System.out.println("请录入匹萨的价格:");
int price = sc.nextInt();
//将录入的信息封装为水果匹萨的对象:
FruitsPizza fp = new FruitsPizza("水果匹萨",size,price,burdening);
p = fp;
}
break;
}
return p;
}
}