主题:“天空之城之全民源代码迁移”
故事背景:“在不久的将来,地球加速运转,随着科技发展,我们进入源世纪,慢慢顺应了星球的运转,我们可以穿梭在外太空,结交星空好友,探索宇宙奥秘...”
目前程序定义主类天空之城,及两个继承类先适应的小行星类和流星类,我们给星体命名,搭建科技桥,及源连接...
第一部分,我们先看看主类的内容 @skyCity 天空之城
使用java基本数据类型 字符串String 整数类型int 浮点类型float
我们java的基本数据类型还可以有 无符号型byte、短整型数据类型short、长整型数据类型long、双精度浮点型double、布尔逻辑型boolean、字符指针char
package skyCity01;
/**
* YicStudio
*
* @ClassName类名: skyCity
* @Description描述:
* @author编程者: 一冲子
* @date日期: 2022/11/18 18:49
* @Blog小猴子: https://blog.youkuaiyun.com/YIC020920/
* @Blog博客园: https://www.cnblogs.com/YICHONG-777/
*/
public class skyCity {
/** 天空之城初始变量 基本数据类型
* @population 人口
*/
private int skyCityId = 1001; //@skyCityId 天空之城ID
String name = "天空之城"; //命名:天空之城
int population = 0; //天空之城人口
float sideX = 1.9866f; //比例边长X
float sideY = 2.7897f; //比例边长Y
int appearYear = 2077; //出现年份
}
第二部分是两个继承类 @asteroid 小行星类 @meteor 流星类
这里我们可以看一下新加的两个构造器里存在一个super(),它可以帮助我们访问超类的构造方法和子类所隐藏的方法。
/**
* @asteroid 小行星类
*/
class asteroid extends skyCity{
private String name; //@name 小行星命名
private int appearYear; //天空之城新增小行星居住地年份
private int asteroidId; //@asteroidId 小行星ID
private double asteroidPopulation; //小行星人口
private String operatingMode = "绕月飞行";
public asteroid(int skyCityId, String name, int population, int appearYear, String name1, int appearYear1, int asteroidId, double asteroidPopulation) {
super(skyCityId, name, population, appearYear);
this.name = name1;
this.appearYear = appearYear1;
this.asteroidId = asteroidId;
this.asteroidPopulation = asteroidPopulation;
}
}
/**
* @meteor 流星类
*/
public class meteor extends skyCity{
String name = "天空之城主流星体";
int meteorId = 20830713;
int appearYear = 2083;
double meteorPopulation = 0; //流星人口
public meteor(String name, int meteorId, int appearYear, double meteorPopulation) {
super();
this.name = name;
this.meteorId = meteorId;
this.appearYear = appearYear;
this.meteorPopulation = meteorPopulation;
}
}
第三部分实现封装
1. 修改属性的可见性来限制对属性的访问(一般限制为private)
2. 对每个值属性提供对外的公共方法访问,也就是创建一对赋取值方法,用于对私有属性的访问
public class meteor extends skyCity{
String name = "天空之城主流星体";
int meteorId = 20830713;
int appearYear = 2083;
double meteorPopulation = 0; //流星人口
public meteor(String name, int meteorId, int appearYear, double meteorPopulation) {
super();
this.name = name;
this.meteorId = meteorId;
this.appearYear = appearYear;
this.meteorPopulation = meteorPopulation;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
public int getMeteorId() {
return meteorId;
}
public void setMeteorId(int meteorId) {
this.meteorId = meteorId;
}
@Override
public int getAppearYear() {
return appearYear;
}
@Override
public void setAppearYear(int appearYear) {
this.appearYear = appearYear;
}
public double getMeteorPopulation() {
return meteorPopulation;
}
public void setMeteorPopulation(double meteorPopulation) {
this.meteorPopulation = meteorPopulation;
}
/** @meteorIntroduce 流星简介 */
void meteorIntroduce(){
System.out.println("当前主流星类使用实参进行命名:"+name+",流星编号是:"+meteorId+",升空年份:"+appearYear+"年!");
System.out.println("以下是使用形参构造器命名的星体...");
meteor m1 = new meteor("美博星",20770328,2080,12.8);
System.out.println("当前泛流星类被命名为:"+m1.name+",流星编号是:"+m1.meteorId+",升空年份:"+m1.appearYear+"年,居住人口:"+m1.meteorPopulation+"万人");
subMeteor sM1 = new subMeteor("白蒂星", 20900829, 2083, 2.7);
System.out.println("当前子流星类被命名为:"+sM1.name+",流星编号是:"+sM1.meteorId+",升空年份:"+sM1.appearYear+"年,居住人口:"+sM1.meteorPopulation+"万人");
System.out.println("当前主流星类的总人口:"+( m1.meteorPopulation + sM1.meteorPopulation )+"万人");
}
}
第四部分@finalClass,主要内容是finalAndStatic,沉着且保持冷静。
/** final 类不能被继承,没有类能够继承 final 类的任何特性。*/
public final class finalClass{
final int number = 10;
/** final 修饰符通常和 static 修饰符一起使用来创建类常量。 */
public static final int NUMBER = 20;
int sumNumber;
boolean constant = true;
public final void modifiers(){
/**
* 父类中的 final 方法可以被子类继承,但是不能被子类重写。
* 声明 final 方法的主要目的是防止该方法的内容被修改。
*/
System.out.println("本方法由final修饰,不可被修改");
sumNumber = number+NUMBER;
if (NUMBER==30){
constant = true;
System.out.println("当前变量为常量,可被修改?"+constant+",运算结果:"+sumNumber);
}else {
constant = false;
System.out.println("当前变量为常量,不可被修改!"+constant+",运算结果:"+sumNumber);
}
}
}
最后是使用主方法执行整个天空之城故事程序...面向对象基本实现.
public class skyCity_main {
public static void main(String[] args) {
System.out.println("我是天空之城主方法...");
/**创建一个对象调用自身的方法*/
skyCity s1 = new skyCity();
s1.range();
s1.exist();
}
}