类和方法
构造方法和方法重载
public class Phone {
String name;
int age;
// 当用户没有写任何形式的构造方法时,系统会自动的为程序提供一个无参构造方法
/*
public Phone() {
System.out.println("我是无参构造");
}
*/
// 如果用户自己编写类构造方法,系统会不再为我们提供默认
public Phone(){}
public Phone(String name){
System.out.println("有参构造");
}
}
在调用过程中,调用main方法的时java虚拟机
public class Phone {
String brand;
int price;
public Phone(){
System.out.println("无参构造");
}
public Phone(String brand,int price){
System.out.println("有参构造");
this.price = price;
this.brand = brand;
}
public void show(){
System.out.println("手机的品牌是:"+brand+","+"价格是:"+price);
}
public static void main(String[] args) {
Phone p1 = new Phone();
Phone p2 = new Phone("华为",2999);
p1.brand = "小米";
p1.price = 1999;
p1.show();
p2.show();
}
}
练习:
import java.util.Scanner;
public class Point {
int x;
int y;
public Point(){}
public Point(int x,int y){
this.x = x;
this.y = y;
}
public void show(){
System.out.println("x:"+x+","+"y:"+y);
}
public void addX(){
x++;
}
public void addY(){
Scanner sca = new Scanner(System.in);
System.out.println("请输入要给y增加的数的大小:");
y += sca.nextInt();
System.out.println("现在的y值是:"+y);
}
public static void main(String[] args) {
// 无参
Point p1 = new Point();
// 有参
Point p2 = new Point(10,15);
p2.show();
p2.addX();
p2.addY();
}
}
public class Rectangle {
double x;
double y;
public Rectangle(double x,double y){
this.x = x;
this.y = y;
}
public double getArea(){
return x*y;
}
public double getPer(){
return (x+y)*2;
}
public void showAll(){
double A = x * y;
double P = (x+y)*2;
System.out.println("矩形的面积是:"+A+", 周长是:"+P);
}
public static void main(String[] args) {
Rectangle r = new Rectangle(10,15);
System.out.println(r.getArea());
System.out.println(r.getPer());
r.showAll();
}
}
public class Vehicle {
double speed;
String type;
public Vehicle(double speed,String type){
this.speed = speed;
this.type = type;
}
public void show(){
System.out.println("车型是:"+type+",现在的速度是:"+speed+"km/h");
}
public void move(){
System.out.println(type+"动了");
}
public void setSpeed(double speed) {
this.speed = speed;
}
public void speedUp(double speed){
this.speed += speed;
System.out.println("现在的速度是:"+this.speed+"km/h");
}
public void speedDown(double speed){
this.speed -= speed;
System.out.println("现在的速度是:"+this.speed+"km/h");
}
public static void main(String[] args) {
Vehicle ve = new Vehicle(30.0,"本田");
ve.show();
ve.move();
ve.setSpeed(60.0);
ve.speedUp(15.0);
ve.speedDown(45.0);
ve.show();
}
}
public class Circle {
float r;
final float PI = 3.14f;
public Circle(float r){
this.r = r;
}
public void circleS(){
float S = PI * r *r;
System.out.println("圆的面积是:"+S);
}
public void circleC(){
float C = PI * 2 *r;
System.out.println("圆的周长是:"+C);
}
public static void main(String[] args) {
Circle c = new Circle(3.6f);
c.circleS();
c.circleC();
}
}
public class Car {
String brand;
String color;
int price;
public Car(){}
public Car(String brand,String color,int price){
this.brand = brand;
this.color = color;
this.price = price;
}
public void show(){
System.out.println("车的品牌是:"+brand+",车的颜色是:"+color+",车的价格是:"+price);
}
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car("大众","黑",580000);
c2.show();
}
}
继承
package com.text01;
public class Person {
private String name;
private int age;
public void eat(){}
public void play(){}
}
package com.text01;
public class Student extends Person{
private int studentId;
public void study(){}
}
package com.text01;
public class Teacher extends Person{
private int teacherId;
public void teach(){}
}
package com.text01;
public class Work extends Person{
private double salary;
public void work(){}
}