扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
Generic.java:
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名、网站空间、营销软件、网站建设、无棣网站维护、网站推广。
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) {
}
}
关键了解接口的作用,你把下面的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) 编写程序实现如下功能:已知Person类包含三个公共成员变量(姓名、性别、年龄)和一个构造方法,
* Student类是Person类的派生类,包含两个新的公共成员变量(学号、班号)、两个公共方法(修改年龄、显示基本信息)及一个构造方法。
* 在测试类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;
}
}
不可否认,我现在是有点闲,所以我就帮你写第一个吧,至于后面两个,我就不写了,看看还有没有其他人有点闲时间,看缘分吧
运行结果:
我的姓名是小李,我的性别是男,我的年龄是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");
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流