Generic.java:
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:主機域名、網(wǎng)站空間、營銷軟件、網(wǎng)站建設(shè)、無棣網(wǎng)站維護、網(wǎng)站推廣。
package com.example.demo;
public interface GenericT {
void get(T t);
}
GenericImpl.java:
package com.example.demo;
public class GenericImplT implements GenericT {
@Override
public void get(T t) {
}
}
關(guān)鍵了解接口的作用,你把下面的Cp改成你要的TestSort就OK樂,Cp是我的類文件名 懶得改了interface Sortble{
public int Compare(Sortble s);
}
class Student implements Sortble{
private int score;
Student(int s){
score=s;
}
public int Compare(Sortble s) {
// TODO Auto-generated method stub
Student ss=null;
if(s instanceof Student){
ss=(Student)s;
}else{
System.out.println("程序出錯,意外退出");
System.exit(0);
}
if(this.getScore()ss.getScore()){
return 1;
}else if(this.getScore()ss.getScore()){
return -1;
}else{
return 0;
}
}
public String toString(){
return ""+getScore();
}
public void setScore(int score) {
this.score = score;
}
public int getScore() {
return score;
}
}
class Rectangle implements Sortble{
private int length,width;
Rectangle(int length,int width){
this.length=length;
this.width=width;
}
public int area(){
return length*width;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int Compare(Sortble s) {
Rectangle ss=null;
// TODO Auto-generated method stub
if(s instanceof Rectangle){
ss=(Rectangle)s;
}else{
System.out.println("程序出錯,意外退出");
System.exit(0);
}
if(this.area()ss.area()){
return 1;
}else if(this.area()ss.area()){
return -1;
}else{
return 0;
}
}
public String toString(){
return ""+area();
}
}
class Sort{
public static void SelectSort(Sortble[] a){
Sortble m=null;
for(int i=0;ia.length-1;i++){//升序
for(int j=i+1;ja.length;j++){
if(a[j].Compare(a[i])0){
m=a[i];
a[i]=a[j];
a[j]=m;
}
}
}
}
}
public class Cp{
Cp(){
Student[] s=new Student[5];
for(int i=0;is.length;i++){
s[i]=new Student((int)(Math.random()*100));
}
Sort.SelectSort(s);
System.out.println("下面是按升序輸出學生成績");
for(int i=0;is.length;i++){
System.out.println(s[i]);
}
Rectangle[] ss=new Rectangle[5];
for(int i=0;iss.length;i++){
ss[i]=new Rectangle((int)(Math.random()*100),(int)(Math.random()*100));
}
Sort.SelectSort(ss);
System.out.println("下面是按升序輸出矩形面積");
for(int i=0;iss.length;i++){
System.out.println(ss[i]);
}
}
public static void main(String[] arg){
new Cp();
}
}
package?TestPerson;
/**
*?(1)?編寫程序?qū)崿F(xiàn)如下功能:已知Person類包含三個公共成員變量(姓名、性別、年齡)和一個構(gòu)造方法,
*?Student類是Person類的派生類,包含兩個新的公共成員變量(學號、班號)、兩個公共方法(修改年齡、顯示基本信息)及一個構(gòu)造方法。
*?在測試類Test1中,定義一組學生對象,并初始化他們的基本信息,然后依次輸出。
*/
public?class?Test1?{
public?static?void?main(String[]?args)?{
Student[]?student?=?new?Student[3];
student[0]?=?new?Student("小李",?"男",?12,?20181101,?01);
student[1]?=?new?Student("小南",?"女",?13,?20001102,?01);
student[2]?=?new?Student("小李",?"男",?12,?20181103,?01);
for(Student?stu?:?student)?{
stu.showInformation();
}
}
}
class?Person?{
public?String?name;
public?String?sex;
public?int?age;
public?Person(String?name,?String?sex,?int?age)?{
super();
this.name?=?name;
this.sex?=?sex;
this.age?=?age;
}
}
class?Student?extends?Person?{
public?long?studentId;
public?long?classId;
public?void?setAge(int?age)?{
age?=?this.age;
}
public?void?showInformation()?{
System.out.println("我的姓名是"?+?name?+?","?+?"我的性別是"?+?sex?+?","?+?"我的年齡是"?+?age?
+?"歲,"?+?"我的學號是"?+?studentId?+?","?+?"我的班號是"?+?classId?+?"班");
}
public?Student(String?name,?String?sex,?int?age,?long?studentId,
long?classId)?{
super(name,?sex,?age);
this.studentId?=?studentId;
this.classId?=?classId;
}
}
不可否認,我現(xiàn)在是有點閑,所以我就幫你寫第一個吧,至于后面兩個,我就不寫了,看看還有沒有其他人有點閑時間,看緣分吧
運行結(jié)果:
我的姓名是小李,我的性別是男,我的年齡是12歲,我的學號是20181101,我的班號是1班
我的姓名是小南,我的性別是女,我的年齡是13歲,我的學號是20001102,我的班號是1班
我的姓名是小李,我的性別是男,我的年齡是12歲,我的學號是20181103,我的班號是1班
class?Main?{
public?static?void?main(String[]?args)?{
IPerson?person?=?new?Person();
person.setFood("西紅柿炒雞蛋");
person.eat();
}
}
interface?IPerson{
void?eat();
void?setFood(String?food);
}
class?Person?implements?IPerson{
private?String?food;
@Override
public?void?eat()?{
System.out.println("正在吃"?+?food);
}
@Override
public?void?setFood(String?food)?{
this.food?=?food;
}
}
public abstract class Pet {
protected int hValue;
protected static final int PENGUIN_VALUE = 5;
protected static final int DOG_VALUE = 5;
abstract void addHV();
abstract void play();
public int gethValue() {
return hValue;
};
}
/*dog*/
public class Dog extends Pet {
@Override
public void addHV() {
hValue = DOG_VALUE;
System.out.println("dog full");
}
@Override
void play() {
System.err.println("pick a frisbee");
}
}
/*Penguin */
public class Penguin extends Pet {
@Override
public void addHV() {
hValue = PENGUIN_VALUE;
System.out.println("penguin full");
}
@Override
void play() {
System.out.println("swimming");
}
}
public class Owner {
void feed(Pet pet) {
pet.addHV();
}
void play(Pet pet) {
pet.play();
}
public static void main(String[] args) {
Owner owner = new Owner();
Pet dog = new Dog();
Pet penguin = new Penguin();
owner.feed(dog);
owner.feed(penguin);
owner.play(dog);
owner.play(penguin);
}
}
interface?Vehicle?{
public?void?Start(String?s);
public?void?stop(String?s);
}
class?Bike?implements?Vehicle?{
@Override
public?void?Start(String?s)?{
System.out.println(s?+?"?bike?start");
}
@Override
public?void?stop(String?s)?{
System.out.println(s?+?"?bike?stop");
}
}
class?Bus?implements?Vehicle?{
@Override
public?void?Start(String?s)?{
System.out.println(s?+?"?bus?start");
}
@Override
public?void?stop(String?s)?{
System.out.println(s?+?"?bus?stop");
}
}
class?interfaceDemo?{
public?static?void?main(String[]?args)?{
Bike?bike?=?new?Bike();
bike.Start("My");
bike.stop("My");
Bus?bus?=?new?Bus();
bus.Start("My");
bus.stop("My");
}
}