package 自主练习;
class Student{
static String schoolName; // 定义静态变量schoolName
}
public class static关键字静态变量 {
public static void main(String[] args) {
Student stu1=new Student(); //创建学生对象
Student stu2=new Student();
Student .schoolName="西安科技大学";
//为静态变量赋值
System.out.println("我的学校是"+stu1.schoolName);
System.out.println("我的学校是"+stu2.schoolName);
}
}
package 自主练习;
public class 静态代码块 {
static{
System.out.println("测试类的静态代码块执行了");
}
public static void main(String[] args) {
Person p1=new Person();
Person p2=new Person();
}
}
class Person{
static String country;
static{
country="china";
System.out.println("person类中的静态代码块执行了");
}
}
package 自主练习;
public class super关键字 {
class Animal{
void shout(){
System.out.println("动物发出叫声");
}
}
class Dog extends Animal{
String name ="犬类";
void shout(){
super.shout();
}
void printName(){
System.out.println("name="+super.name);
}
}
public static void main(String[] args) {
Dog dog=new Dog();
dog.shout();
dog.printName();
}
}
private double diameter;
public void sphere()
{
this.diameter=1.0;
}
public void sphere(double d)
{
this.diameter=d;
}
public void setDameter(double d)
{
this.diameter=d;
}
public double getDiameter()
{
return this.diameter;
}
public double volume()
{
return
4*Math.PI*Math.pow(this.diameter/2,3)/3;
}
public double area()
{
return 4*Math.PI*Math.pow(this.diameter/2,2);
}
public String toString()
{
String out ="该球体的直径为:"+ this.diameter+"\n"+
"该球体的表面积为:"+this.area() + "\n" +
"该球体的体积为:"+ this.volume();
return out;
}
public static void main(String[] args)
{
Scanner scan= new Scanner(System.in);
sphere sphere1 = new sphere();
sphere sphere2 = new sphere();
System.out.println("sphere1: ”+ sphere1 +"
+
“\n”);
System.out. println("sphere2: ”+ sphere2 +"
+
“\n”);
System.out.println("sphere1和sphere2分别调用无参构造方法"+"和带一个参数的构造方法进行初始化.");
System.out.print("现在,请输入一个数作为球sphere1 的直径值");
sphere1.setDameter(scan.nextDouble());
System.out.println("\n" + "更改过的sphere1:");
在这里插入代码片
public class pp42 {
private String name;
private int age;
private int mage;
public void dog (String dogname, int dogage)
{
name = dogname;
age = dogage;
}
public String getName()
{
return name ;
}
public int getAge()
{
return age;
}
public int getMage()
{
mage = age*7;
return mage;
}
public String toString()
{
return (name+"\t"+age+"\t"+mage);
}
public class Kennel
{
public void main(String[] args)
{
dog dog1 = new dog();
dog dog2 = new dog();
dog dog3 = new dog();
dog dog4 = new dog();
dog dog5 = new dog();
dog1. getMage() ;
dog2. getMage();
dog3. getMage();
dog4. getMage();
dog5. getMage();
System. out. println("The information of the dog:" );
System. out. println (dog1);
System. out. println (dog2);
System. out. println (dog3);
System. out. println (dog4);
System. out. println (dog5);
}
}
}
在这里插入代码片
``
public class Box {
private double height, width, thickness;
private boolean full;
void Box(double height, double width, double thickness){
this.height=height;
this.width=width;
this.thickness=thickness;
full=false;
}
public double getHeight(){
return height;
}
public double getWidth(){
return width;
}
public double thickness(){
return thickness;
}
public boolean getFull(){
return full;
}
public void setHeight(double height){
this.height=height;
}
public void setWidth(double width){
this.width=width;
}
public void setThickness(double thickness){
this.thickness=thickness;
}
public void setFull1(boolean full){
this.full=full;
}
public String toString(){
String s="";
if(getFull()==true)
s="The height of the box is "+getHeight()+";"
+"The
height of the box is "+getHeight()+";"
+"The
height of the box is "+getHeight()+";"
+"and the
box is full";
else
s="The height of the box is "+getHeight()+";"
+"The
height of the box is "+getHeight()+";"
+"The
height of the box is "+getHeight()+";"
+"and the
box is not full";
return s;
}
public static void main(String args[]){
Box b1=new Box();
Box b2=new Box();
System.out.println(b1);
System.out.println(b2);
b1.setFull1(true);
b1.setHeight(15);
b1.setWidth(13);
b1.setThickness(14);
b2.setHeight(6);
b2.setWidth(4);
b2.setThickness(8);
System.out.println(b1);
System.out.println(b2);
在这里插入代码片
private
String number;
private String name;
private String publisher;
private String author;
public void Book(String number, String name, String publisher, String author) {
this.number = number;
this.name = name;
this.publisher = publisher;
this.author = author;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String toString() {
return "|--书名:" + this.getName() + "\n\t" + "|--作者:" + this.getAuthor() +
"\n\t" + "|--出版社:" + this.getPublisher() + "\n\t" + "|--书号:" + this.getNumber();