-
package com.imooc.objectproject.sample3;
//张三开发的排序算法类
public class Algorithm {
/*
*传入五个数,控制台打印升序效果
* @param a 第一个数
* @param b 第二个数
* @param c 第三个数
* @param d 第四个数
* @param e 第五个数
*/
public void sort(int a , int b , int c , int d , int e){
//冒泡排序法
int [] arr = {a,b,c,d,e} ;
for(int i = 0;i < arr.length - 1;i++){
for(int j =0;j < arr.length - 1 - i;j++){
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(int i = 0;i < arr.length;i++){
System.out.println(arr[i] + " ");
}
}
public static void main(String[] args) {
Algorithm algorithm = new Algorithm();
algorithm.sort(82,88,988,223,184);
}
}
不用谢,请叫我红领巾~
查看全部 -
car类 public abstract class Car { protected int type=0;//車的類型 1載貨 2 載客 3 載貨載客都有 protected int id=0;//車編號 protected String name="";//車名稱 protected int carryWeight=0;//車的載重量 5T 輕型 5T-20T重型 protected int carryPeople=0;//載客量 小客車5人以下 大客車10人以上 protected int rent=0;//租金 每天 public int getRent(){ return this.rent; } public String getName(){ return this.name; } public int getCarryPeople(){ return this.carryPeople; } public int getCarryWeight(){ return this.carryWeight; } public void setArg(int type,int id,String name,int carryWeight,int carryPeople,int rent){ this.type=type; this.id=id; this.name=name; this.carryWeight=carryWeight; this.carryPeople=carryPeople; this.rent=rent; } public abstract void show(); }
public class PeopleCar extends Car { public PeopleCar(int id,String name,int carryPeople,int rent){ setArg(2,id,name,0,carryPeople,rent); } public void show(){ System.out.println(this.id+". \t"+this.name+" \t"+this.rent+"元/天 \t"+ "載人:"+this.carryPeople+"人"); } }
public class WeightCar extends Car { public WeightCar(int id,String name,int carryWeight,int rent){ setArg(1,id,name,carryWeight,0,rent); } public void show(){ System.out.println(this.id+". \t"+this.name+" \t"+this.rent+"元/天 \t"+ "載貨:"+this.carryWeight+"頓"); } }
public class WorPopCar extends Car{ public WorPopCar(int id,String name,int carryWeight,int carryPeople,int rent){ setArg(3,id,name,carryWeight,carryPeople,rent); } public void show(){ System.out.println(this.id+". \t"+this.name+" \t"+this.rent+"元/天 \t"+ "載人:"+this.carryPeople+"人 載貨:"+this.carryWeight+"頓"); } }
import java.util.Scanner; public class Test { private static Car[] carArr; public static void main(String[] args) { carArr=new Car[6]; carArr[0]=new PeopleCar(1,"歐迪A4",4,500); carArr[1]=new PeopleCar(2,"馬自達6",4,400); carArr[2]=new WorPopCar(3,"皮卡雪6",2,4,450); carArr[3]=new PeopleCar(4,"金龍",20,800); carArr[4]=new WeightCar(5,"松花江",4,400); carArr[5]=new WeightCar(6,"依維柯",20,1000); initzuche(); } private static void initzuche(){ Scanner in = new Scanner(System.in); System.out.println("欢迎使用噠噠租车系统:"); System.out.println("您是否要租車:1是 0否"); int a = in.nextInt(); if(a==1){//進入租車系統 System.out.println("您可租車的類型及其價目表:"); System.out.println("序號 \t汽車名稱 \t租金 \t容量"); for (Car icar:carArr) { icar.show(); } System.out.println("請輸入您要租汽車的數量:"); int num = in.nextInt(); Car[] selectArr = new Car[num]; //統計一起做 String pop=""; int popnum=0; String wei=""; double weinum=0; int allprice=0; for(int i=1;i<=num;i++){ System.out.println("請輸入第"+i+"輛車的序號:"); int xh= in.nextInt(); Car tmp=carArr[xh-1]; selectArr[i-1]=tmp; int tmppop=tmp.getCarryPeople(); int tmpwei=tmp.getCarryWeight(); popnum+=tmppop; weinum+=tmpwei; if(tmppop>0){ pop+=tmp.getName()+" "; } if(tmpwei>0){ wei+=tmp.getName()+" "; } allprice+=tmp.getRent(); } System.out.println("請輸入租車天數:"); double day = in.nextInt(); System.out.println("您的賬單:"); System.out.println("***可載人的車有:"); System.out.println(" "+pop+" 共載人:"+popnum+"人"); System.out.println("***載貨的車有:"); System.out.println(" "+wei+" 共載貨:"+weinum+"頓"); System.out.println("***租車總價格:"+allprice*day+"元"); }else{ initzuche(); } } }
大家看下,有问题找我
查看全部 -
静态内部类: 1、 静态内部类不能直接访问外部类的非静态成员,但可以通过 new 外部类().成员 的方式访问 2、 如果外部类的静态成员与内部类的成员名称相同,可通过“类名.静态成员”访问外部类的静态成员;如果外部类的静态成员与内部类的成员名称不相同,则可通过“成员名”直接调用外部类的静态成员 3、 创建静态内部类的对象时,不需要外部类的对象,可以直接创建 内部类 对象名= new 内部类();查看全部
-
练习笔记:package exercise; public abstract class Shape{ public abstract double perimeter(); public abstract double area(); } public class Rectangle extends Shape{ private double length; private double width; public Rectangle(double length, double width){ this.length = length; this.width = width; } public double perimeter(){ return (length + width) * 2; } public double area(){ return length * width; } } public class Circle extends Shape{ private final double PI = 3.1415926; private double radius; public Circle(double radius){ this.radius = radius; } public double perimeter(){ return 2 * PI * radius; } public double area(){ return PI * radius * radius; } } public class Test { public static void main(String[] args) { Shape rec = new Rectangle(2, 3); Shape cir = new Circle(4); System.out.println(rec.perimeter() + " " + rec.area()); System.out.println(cir.perimeter() + " " + cir.area()); } }查看全部
-
package yeyu; public class Trafic { public int loadPerson; public void function(){ System.out.println("交通工具具有载人的功能"); } } package yeyu; public class Bus extends Trafic { public void function(){ System.out.println("公共汽车在陆上载客:"+loadPerson+"人"); } } package yeyu; public class Plane extends Trafic{ public String loadPerson; public void function(){ System.out.println("飞机在空中载客:"+loadPerson+"人"); } } package yeyu; public class Board extends Trafic { public void function(){ System.out.println("轮船在海上载客:"+loadPerson+"人"); } } package yeyu; public class TraficText { public static void main(String[] args) { // TODO Auto-generated method stub Trafic load1=new Bus(); Trafic load2=new Board(); Plane load3=new Plane(); load1.loadPerson=40; load2.loadPerson=200; load3.loadPerson="XXX"; load1.function(); load2.function(); load3.function(); } }查看全部
-
new 一个对象是实例吗? 对,是实例,你说的没错。可以把这个new出来的对象叫做实例,说白了就是这个new出来的“东西”,叫它对象也可以,叫它实例也可以,对象和实例在这个角度上来讲是等价的。 这样: Java中使用 new关键字 加上 构造方法,来创建一个对象,下面是一个名为Cat的类, public class Cat { public Cat() { System.out.println("这是构造方法"); } } 使用new + 构造方法 来创建一个对象,那么也就是 Cat c = new Cat(); 前半部分,Cat c 的意思是,在内存中分配一个变量,名字叫c,这个变量是Cat类型的,它的值是什么? 一会儿在说; 后半部分,new Cat(); 这就是new关键字和构造方法来创建一个对象,Cat()是构造方法的名字没错吧?想造出一个对象来,就这么写,语法规定的,没有为什么; new Cat(); 说明 new这个Cat类的一个对象,程序运行的时候,会调用构造方法Cat(),等这个构造方法执行完了,这个Cat类型的对象也就造出来了,真正的出现在内存当中了; 使用new关键字造出来的对象,被分配在内存的堆区(heap),而且等这个对象真正出来之后,还会做一件重要的事情: 我们这个对象是被分配在内存中的,那么内存地方大了,这个对象在哪里呢?怎么找到它呢?new关键字创建出一个对象之后,会把这个对象在内存中的地址返回,通过这个地址就可以找到这个对象,那么我们上面的写法, Cat c = new Cat(); 意思就是说,把这个对象在内存中的地址 赋值 给变量c,这就是Java中引用概念,c就叫做引用,或者叫引用变量,或者直接叫变量,没问题,都是它; c的值就是一个内存地址,或者叫引用地址,通过这个地址,就可以准确的找到我们刚才创建出来的对象,以后我们要使用这个对象做一些事情,调用此对象的方法什么的,都用过这个引用,ok? ---- 注意,我再说一遍,好多人都搞不清楚,这个c到底是对象还是引用,好多人说c就是Cat类的一个实例,这是非常错误的,c就是引用,不是对象!我们new出来的这个东西,真正在内存中的这个东西叫做对象,叫做实例 个人理解:对象是内存中类的属性集合,变量是这个集合在内存中的引用地址查看全部
-
面向对象的三大特性:封装、继承、多态 封装: 将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问。 好处:只能通过规定的方法访问数据;隐藏类的实例细节,方便修改和实现。 封装实现方法:1)、修改属性可见性。2)、创建getter\setter方法。3)、在getter\setter中加入属性控制语句(对属性值的合法性进行判断)。 封装 : 1.类变成private float screen; 2. 加get/set public float getScreen(){ return screen; } public void setScreen(float newScreen){ screen=newScreen; } 3. main调用: phone.setscreen(6.0f); System.out.printly(phone.getScreen()); 好处:只能通过规定的方法访问数据;隐藏类的实例细节,方便修改和实现。查看全部
-
父类
Car
子类
PassengerCar
Pickup
Truck
主程序
Tset
查看全部 -
super关键字的应用: 1.子类的构造过程中必须调用其父类的构造方法 2.如果子类的构造方法没有显示调用父类的构造方法,则系统会默认调用父类的无参构造方法。 3.如果显示的调用构造方法,必须在子类的构造方法的第一行,super()。 4.如果子类的构造方法中既没有显示调用父类的构造方法,父类又没有无参的构造方法,则编译就会报错查看全部
-
所有/类的对象/共享静态变量 推荐用类名访问HelloWorld.hobby 【请称呼static为公共炮友。】查看全部
-
1、继承的概念:继承是类与类的一种关系;Java中的继承是单继承,只有一个父类。 2、继承的好处:子类直接拥有父亲的所有属性和方法。---private实现的无效! 代码可复用。 class 子类 extends 父类{……}查看全部
-
package com.imooc;
public class SCar {
public int number;
public String name;
public int money;
SCar(int number,String name,int money){
this.number=number;
this.name=name;
this.money=money;
}
}
package com.imooc;
public class Car extends SCar {
public int person;
Car(int number,String name,int money,int person){
super(number, name, money);
this.person=person;
}
@Override
public String toString() {
return number + "\t" + name + "\t" + money + "元/天" +"\t" + "载人:" + person + "人";
}
}
package com.imooc;
public class Pickup extends Car {
int goods;
Pickup(int number, String name, int money, int person, int goods) {
super(number, name, money, person);
this.goods = goods;
}
@Override
public String toString() {
return number + "\t" + name + "\t" + money + "元/天" + "\t" + "载人:" + person + "人," + "载货:" + goods + "吨";
}
}
package com.imooc;
public class Truck extends SCar {
public int goods;
Truck(int number,String name,int money,int goods){
super(number,name,money);
this.goods=goods;
}
@Override
public String toString() {
return number + "\t" + name + "\t" + money + "元/天" + "\t" + "载货:" + goods + "吨";
}
}
package com.imooc;
import java.util.Scanner;
public class Initial {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎使用达达租车系统:");
System.out.println("您是否要租车:1是 0否");
Scanner input1=new Scanner(System.in);
int sc=input1.nextInt();
if(sc==0){
input1.close();
return;
}
if(sc==1){
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 容量");
Car c1=new Car(1,"奥迪A4",500,4);
//System.out.println(c1);
Car c2=new Car(2,"马自达6",400,4);
//System.out.println(c2);
Pickup c3=new Pickup(3,"皮卡雪6",450,4,2);
//System.out.println(c3);
Car c4=new Car(4,"金龙",800,20);
//System.out.println(c4);
Truck c5=new Truck(5,"松花江",400,4);
//System.out.println(c5);
Truck c6=new Truck(6,"依维柯",1000,20);
//System.out.println(c6);
/* SCar[] sc1 = {new Car(1,"奥迪A4",500,4),new Car(2,"马自达6",400,4),new Pickup(3,"皮卡雪6",450,4,2),
new Car(4,"金龙",800,20),new Truck(5,"松花江",400,4), new Truck(6,"依维柯",1000,20)
};
for(SCar sc2 : sc1){
System.out.println(sc1);
}*/
SCar[] sc1 = {c1,c2,c3,c4,c5,c6};
for(SCar sc2 : sc1){
System.out.println(sc2);
}
System.out.println("请输入您要租汽车的数量:");
Scanner input2=new Scanner(System.in);
int zongshu = input2.nextInt();
int[] xuhao = new int[zongshu];
for(int i=0;i<zongshu;i++){
System.out.println("请输入第"+ (i+1)+"辆车的序号:");
Scanner input3 = new Scanner(System.in);
int no = input3.nextInt();
xuhao[i] = no;
}
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
int rent1=0,rent2=0,rent3=0,rent4=0,rent5=0,rent6=0,p1=0,p2=0,p3=0,p4=0,g1=0,g2=0,g3=0;
int rentall=0;
int pall=0;
int gall=0;
//载人车
for(int i=0;i<zongshu;i++){
if(xuhao[i]==1){
System.out.print(c1.name + " ");
rent1 = c1.money;
p1 = c1.person;
}
if(xuhao[i]==2){
System.out.print(c2.name + " ");
rent2 = c2.money;
p2 = c2.person;
}
if(xuhao[i]==3){
System.out.print(c3.name + " ");
rent3 = c3.money;
p3 = c3.person;
}
if(xuhao[i]==4){
System.out.print(c4.name + " ");
rent4 = c4.money;
p4 = c4.person;
}
}
pall = p1+p2+p3+p4;
System.out.println("共载人:" + pall);
//载物车
System.out.println("***可载物的车有:");
for(int i=0;i<zongshu;i++){
if(xuhao[i]==3){
System.out.print(c3.name + " ");
g1 = c3.goods;
}
if(xuhao[i]==5){
System.out.print(c5.name + " ");
rent5 = c5.money;
g2 = c5.goods;
}
if(xuhao[i]==6){
System.out.print(c3.name + " ");
rent6 = c6.money;
g3 = c6.goods;
}
}
gall = g1+g2+g3;
System.out.println("共载物:" + gall);
rentall=rent1+rent2+rent3+rent4+rent5+rent6;
System.out.println("***租车总价格:" + rentall + "元");
}
else{
System.out.println("感谢使用本系统!");
}
}
}
查看全部 -
/** * @Author 王权 */ abstract class Car { String name;//名称 int rentPrice;//租金 int personCapacity;//载人量 int goodsCapacity;//载货量 } class LoadPersonCar extends Car{ public LoadPersonCar(String name, int rentPrice,int personCapacity) { this.name = name; this.rentPrice = rentPrice; this.personCapacity = personCapacity; } } class LoadGoodsCar extends Car{ public LoadGoodsCar(String name, int rentPrice,int goodsCapacity) { this.name = name; this.rentPrice = rentPrice; this.goodsCapacity = goodsCapacity; } } class LoadPersonAndGoodsCar extends Car{ public LoadPersonAndGoodsCar(String name, int rentPrice,int personCapacity,int goodsCapacity) { this.name = name; this.rentPrice = rentPrice; this.personCapacity = personCapacity; this.goodsCapacity = goodsCapacity; } } class AudiA4 extends LoadPersonCar{ public AudiA4(String name, int rentPrice,int goodsCapacity) { super(name, rentPrice,goodsCapacity); } } class MaZiDa6 extends LoadPersonCar { public MaZiDa6(String name, int rentPrice,int personCapacity) { super(name, rentPrice,personCapacity); } } class PiKaXue6 extends LoadPersonAndGoodsCar { public PiKaXue6(String name, int rentPrice,int personCapacity,int goodsCapacity) { super(name, rentPrice,personCapacity,goodsCapacity); } } class JinLong extends LoadPersonCar { public JinLong(String name, int rentPrice,int personCapacity) { super(name, rentPrice,personCapacity); } } class SongHuaJiang extends LoadGoodsCar { public SongHuaJiang(String name, int rentPrice,int goodsCapacity) { super(name, rentPrice,goodsCapacity); } } class YiWeiKe extends LoadGoodsCar { public YiWeiKe(String name, int rentPrice,int goodsCapacity) { super(name, rentPrice,goodsCapacity); } } import com.sun.org.apache.xpath.internal.SourceTree; import java.util.Scanner; /** * Created by Administrator on 2019/2/15 0015. */ public class DaDaRentCarSystem { public static void main(String[] args) { //初始化汽车种类 Car[] carArray = new Car[6]; carArray[0] = new AudiA4("奥迪A4",500,4); carArray[1] = new MaZiDa6("马自达6", 400, 4); carArray[2] = new PiKaXue6("皮卡雪6", 450, 4, 2); carArray[3] = new JinLong("金龙", 800, 20); carArray[4] = new SongHuaJiang("松花江", 400, 4); carArray[5] = new YiWeiKe("依维柯", 1000, 20); Scanner in = new Scanner(System.in); System.out.println("欢迎使用答答租车系统:"); System.out.println("您是否要租车:1是 0否"); int isRentCar = in.nextInt(); float priceCount = 0.0f; int personCount = 0; float goodsCount = 0.0f; if (isRentCar == 1) { System.out.println("您可租车的类型及其价目表:"); System.out.println("序号\t汽车名称\t租金\t容量\t"); for (int i = 0;i<carArray.length;i++) { Car car = carArray[i]; if (car instanceof LoadPersonCar) { System.out.println(i +".\t" +car.name+"\t"+car.rentPrice+"元/天\t" + "载人:" + car.personCapacity +"人\t"); } else if (car instanceof LoadGoodsCar) { System.out.println(i +".\t" +car.name+"\t"+car.rentPrice+"元/天\t" + "载货:" + car.goodsCapacity +"吨\t"); } else { System.out.println(i +".\t" +car.name+"\t"+car.rentPrice+"元/天\t" + "载人:" + car.personCapacity +"人 " +"载货:" + car.goodsCapacity +"吨\t"); } } System.out.println("请输入您要租汽车的数量:"); int rentNum = in.nextInt(); Car[] rentCar = new Car[rentNum]; for (int i = 0;i<rentNum;i++) { System.out.println("请输入第" + i +"辆汽车的序号:"); int seq = in.nextInt(); rentCar[i] = carArray[seq]; } System.out.println("请输入租车天数:"); int day = in.nextInt(); for (Car car : rentCar) { if (car instanceof LoadPersonCar) { personCount += car.personCapacity; priceCount += car.rentPrice * day; } else if (car instanceof LoadGoodsCar) { goodsCount += car.goodsCapacity; priceCount += car.rentPrice * day; } else { personCount += car.personCapacity; goodsCount += car.goodsCapacity; priceCount += car.rentPrice * day; } } System.out.println("您的账单:"); System.out.println("***可载人的车有:"); for (int i = 0;i<rentCar.length;i++) { if (rentCar[i] instanceof LoadPersonCar || rentCar[i] instanceof LoadPersonAndGoodsCar) { System.out.print(rentCar[i].name +" "); } } System.out.println("共载人:" + personCount + "人" ); System.out.println("***可载货的车有:"); for (int i = 0;i<rentCar.length;i++) { if (rentCar[i] instanceof LoadGoodsCar || rentCar[i] instanceof LoadPersonAndGoodsCar) { System.out.print(rentCar[i].name + " "); } } System.out.println("共载货:" + goodsCount + "吨" ); System.out.println("租车总价格:" +priceCount+"元" ); } } }
查看全部 -
完全不懂运用查看全部
-
封装:将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问。 好处:1、只能通过规定的方法访问数据。 2、隐藏类的实例细节,方便修改和实现 封装的步骤:1、修改属性的可见性,设为private 2、创建个getter/setter方法用于属性的读写 3、在getter/setter方法中加入属性控制语句,对属性值的合法性进行判断 众所周知,JAVA是面向对象的一门语言,每个对象都有各自的属性和方法.但是有的属性涉及安全因素或秘密因素不能让外面的程序调用,所以就用到了private, 经过它定义的变量只能在此对象内部调用,外部程序就无法调用,也就是说只能供其自身调用,比如某个人是一个对象,那么他的血液就肯定是私有变量,只能供其自身调用来维持人的状态.而如果将血液定义成public(公共的)的话,随便其他人都可以改变他血液的状态,你随便把他的血液值设为逆流,那他不就挂了.查看全部
举报