目录
01:设计一个Dog类,有名字、颜色和年龄属性,提供无参的构造方法和一个有参的构造方法,定义输出方法show()显示其信息。
02:写一个人的类。
03:写一个汽车类。
04:写一个课程类。
05:定义一个类,用于描述坐标点。
06:定义一个圆类型。
07:编程创建一个Box类,在其中定义三个变量表示一个立方体的长、宽和高,定义一个方法求立方体的体积。
使用有参构造方法创建一个对象,并输出该对象的信息。
package com.nanchu.classpractice.way;
/**
* @Author 南初
* @Create 2024/1/17 12:48
* @Version 1.0
*/
public class Dog {
// 01:设计一个Dog类,有名字、颜色和年龄属性,
private String name;
private String color;
private int age;
// 提供无参的构造方法和一个有参的构造方法,
public Dog() {
}
public Dog(String name, String color, int age) {
this.name = name;
this.color = color;
this.age = age;
}
// 定义输出方法show()显示其信息。
public void show(){
System.out.println("名字:"+this.name+",颜色:"+this.color+",年龄:"+this.age);
}
}
// 使用有参构造方法创建一个对象,并输出该对象的信息。
@Test
public void dog(){
Dog dog1 = new Dog("小白","黑色",2);
dog1.show();
}
属性:名字,性别,年龄;提供无参的构造方法和一个有参的构造方法
方法:(1)自我介绍的方法(2)吃饭的方法
使用有参构造方法创建一个对象:姓名为“张三”,性别为“男”,年龄25.
package com.nanchu.classpractice.way;
/**
* @Author 南初
* @Create 2024/1/17 12:56
* @Version 1.0
*/
public class Person {
// 属性:名字,性别,年龄;
// 提供无参的构造方法和一个有参的构造方法
private String name;
private char sex;
private int age;
public Person() {
}
public Person(String name, char sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
// 方法:(1)自我介绍的方法(2)吃饭的方法
public void mySelf(){
System.out.println("我的名字是:"+this.name+",性别:"+this.sex+",年龄"+this.age);
}
public void eat(String food){
System.out.println("今天我吃:"+food);
}
}
// 使用有参构造方法创建一个对象:姓名为“张三”,性别为“男”,年龄25.
@Test
public void person(){
Person person = new Person("张三",'男',25);
person.mySelf();
person.eat("鸡腿");
}
属性:品牌;车长;颜色;价格;
提供无参的构造方法和一个有参的构造方法
使用有参构造方法创建五个对象:“捷达”,“宝马”,“劳斯莱斯”,“科鲁兹”,“迈锐宝”
package com.nanchu.classpractice.way;
/**
* @Author 南初
* @Create 2024/1/17 13:05
* @Version 1.0
*/
public class Car {
// 属性:品牌;车长;颜色;价格;
private String brand;
private double carLength;
private String color;
private double price;
// 提供无参的构造方法和一个有参的构造方法
public Car() {
}
public Car(String brand, double carLength, String color, double price) {
this.brand = brand;
this.carLength = carLength;
this.color = color;
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", carLength=" + carLength +
", color='" + color + '\'' +
", price=" + price +
'}';
}
}
// 使用有参构造方法创建五个对象:“捷达”,“宝马”,“劳斯莱斯”,“科鲁兹”,“迈锐宝”
@Test
public void carTest(){
Car car1 =new Car("捷达",5.4,"白色",150000);
Car car2 =new Car("宝马",5.5,"黑色",200000);
Car car3 =new Car("劳斯莱斯",6.0,"银白色",2000000);
Car car4 =new Car("科鲁兹",5.1,"红色",10000);
Car car5 =new Car("迈锐宝",5.3,"黑色",15000);
System.out.println(car1.toString());
System.out.println(car2.toString());
System.out.println(car3.toString());
System.out.println(car4.toString());
System.out.println(car5.toString());
}
属性:课程名;学时;任课老师;
提供无参的构造方法和一个有参的构造方法
使用有参构造方法创建五个对象:“c语言”,“Java编程”,“php网络编程”,“c++”,“数据结构”
package com.nanchu.classpractice.way;
/**
* @Author 南初
* @Create 2024/1/17 13:15
* @Version 1.0
*/
public class Course {
// 属性:课程名;学时;任课老师;
private String courseName;
private double classHour;
private String teacher;
// 提供无参的构造方法和一个有参的构造方法
public Course() {
}
public Course(String courseName, double classHour, String teacher) {
this.courseName = courseName;
this.classHour = classHour;
this.teacher = teacher;
}
@Override
public String toString() {
return "Course{" +
"courseName='" + courseName + '\'' +
", classHour=" + classHour +
", teacher='" + teacher + '\'' +
'}';
}
}
// 使用有参构造方法创建五个对象:“c语言”,“Java编程”,“php网络编程”,“c++”,“数据结构”
@Test
public void courseTest(){
Course course1 = new Course("C语言",50,"李老师");
Course course2 = new Course("Java编程",60,"王老师");
Course course3 = new Course("php网络编程",40,"张老师");
Course course4 = new Course("c++",55,"贾老师");
Course course5 = new Course("数据结构",70,"肖老师");
System.out.println(course1.toString());
System.out.println(course2.toString());
System.out.println(course3.toString());
System.out.println(course4.toString());
System.out.println(course5.toString());
}
0——————>X
|
|
| P(X,Y)
|
|
Y
(1)提供无参的构造方法和一个有参的构造方法
(2)具有计算当前点到原点距离的功能
(3)求到任意一点(m,n)的距离
(4)求到任意一点(Point p)的距离
(5)具有坐标点显示功能,显示格式(x,y)
package com.nanchu.classpractice.way;
/**
* @Author 南初
* @Create 2024/1/17 13:33
* @Version 1.0
*/
public class Point {
public double x,y;
public Point() {
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
}
package com.nanchu.classpractice.way;
/**
* @Author 南初
* @Create 2024/1/17 13:24
* @Version 1.0
*/
public class Axis {
private double x,y,m,n;
//(1)提供无参的构造方法和一个有参的构造方法
public Axis() {
}
public Axis(double x, double y, double m, double n) {
this.x = x;
this.y = y;
this.m = m;
this.n = n;
}
// (2)具有计算当前点到原点距离的功能
public void function1(){
double distance;
distance = Math.sqrt(Math.pow(this.x,2)+Math.pow(this.y,2));
System.out.println("距离原点距离:"+distance);
}
// (3)求到任意一点(m,n)的距离
public void function2(){
double distance;
distance =Math.sqrt(Math.pow(this.m-this.x,2)+Math.pow(this.n-this.y,2));
System.out.println("("+this.x+","+this.y+")"+"到("+this.m+","+this.n+")的距离为:"+distance);
}
// (4)求到任意一点(Point p)的距离
public void function3(Point p){
double dis;
dis = Math.sqrt(Math.pow(p.x-this.x,2)+Math.pow(p.y-this.y,2));
System.out.println("("+this.x+","+this.y+")"+"到点("+p.x+","+p.y+")的距离为:"+dis);
}
// (5)具有坐标点显示功能,显示格式(x,y)
public void function4(){
System.out.println("("+this.x+","+this.y+")");
}
}
@Test
public void AxixTest(){
// 使用有参方法构造
Axis axis = new Axis(1,1,4,5);
Point p = new Point(2,2);
// 四个功能测试
axis.function1();
axis.function2();
axis.function3(p);
axis.function4();
}
属性:半径
提供无参的构造方法和一个有参的构造方法
提供显示圆周长功能的方法
提供显示圆面积的方法
使用有参构造方法创建一个对象并计算圆的周长和面积。
package com.nanchu.classpractice.way;
/**
* @Author 南初
* @Create 2024/1/17 13:56
* @Version 1.0
*/
public class Circle {
// 属性:半径
private double radius;
// 提供无参的构造方法和一个有参的构造方法
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
// 提供显示圆周长功能的方法
public void Perimeter(){
double perimeter;
perimeter = 2*3.14*this.radius;
System.out.println("该圆周长为:"+perimeter);
}
// 提供显示圆面积的方法
public void Area(){
double area;
area = 3.14*this.radius*this.radius;
System.out.println("该圆的面积为:"+area);
}
}
@Test
public void circleTest(){
Circle c = new Circle(3);
c.Perimeter();
c.Area();
}
提供无参的构造方法和一个有参的构造方法
使用有参构造方法创建一个对象,求给定尺寸的立方体的体积。
package com.nanchu.classpractice.way;
/**
* @Author 南初
* @Create 2024/1/17 14:02
* @Version 1.0
*/
public class Box {
// 在其中定义三个变量表示一个立方体的长、宽和高
private double length;
private double width;
private double height;
// 提供无参的构造方法和一个有参的构造方法
public Box() {
}
// 定义一个方法求立方体的体积。
public Box(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}
public void Volume(){
double volume=0;
volume = this.length*this.width*this.height;
System.out.println("该长方体体积为:"+volume);
}
}
@Test
public void boxTest(){
// 使用有参构造方法创建一个对象,求给定尺寸的立方体的体积。
Box box = new Box(2,3,4);
box.Volume();
}