包含javasettype的词条

博主:adminadmin 2023-01-14 10:18:11 318

今天给各位分享javasettype的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java设计一个动物类

package com.animal//定义包, 非注释第一句,不是会包红

public class Animal{//动物类

private String type;//属性1

private String sex ;//属性2

public Animal(){//默认无参构造

setType("老虎");//初始化

setSex("母");

}

public Animal(String type,String sex){//带参构造

setType(type);//初始化

setSex(sex);

}

public void setType(String type){//set访问器修改属性

this.type = type;

}

public String getType(){//get访问器 得到属性

return this.type;

}

public void setSex(String sex){

this.sex = sex;

}

public String getSex(){

return this.sex;

}

public void eat (){//吃方法

System.out.println("我是一只"+getType()+",性别是"+getSex()+",正在吃东西")

}

public void eat (String food){//吃方法的重载

System.out.println("我是一只"+getType()+",性别是"+getSex()+",正在吃"+food)

}

public void sleep (){//睡方法

System.out.println("我是一只"+getType()+",性别是"+getSex()+",正在睡觉")

}

public void sleep (String time){//睡方法重载

System.out.println("我是一只"+getType()+",性别是"+getSex()+",正在睡觉在"+time)

}

}

===============接口================

public interface function(){

public String say();//say方法

}

===============分隔线===============

public class Dog extends Animal implements function {//继承

public Dog(){

super();//一定要在子类构造第一句

}

public Dog(String type, String sex){

super(type,sex); //同上

}

public String say(){//重写 接口中的方法

return "我是一只性别为"+getSex+"的"+getType;

}

}

===================main================

public class Test{

public static void main(String []args){//main

Animal a = new Animal();//无参的animal

a.eat();

a.sleep();

a.eat("鬣狗");

a.sleep("白天");

System.out.println();

//带参

a = new Animal("吉娃娃","公");

a.eat();

a.sleep();

a.eat("狗粮");

a.sleep("晚上");

System.out.println();

Dog dog = new Animal("二哈","公");//animal 类接受 一个 子类dog 对象

dog.eat();

dog.sleep();

dog.eat("狗粮");

dog.sleep("晚上");

System.out.println();

dog = new Dog("小老头","母");

dog.eat();

dog.sleep();

dog.eat("狗粮");

dog.sleep("晚上");

System.out.println(dog.say());

}

}

关于java中内部类引用外部类的问题代码如下

因为你使用system.out.println这个方法输出个对象的时候,因为只能输出字符串,所以系统就会默认调用类中的toStringf方法,因为这个方法是源自object类的,所以几乎所有的类都有这个方法。而你的代码重写了这个方法,所以输出的时候就调用了这个方法,然后按你的规则进行了输出。

比如

public class Test{

    public Test(){}

    public void toString(){

        System.out.println("----test------");

    }

    //假设:Test test=new Test();

     //   System.out.println(test); 

    //输出的结果就是"----test------"

 }

用java继承编写三个类,要求如图

package school;

public class Test {

public static void main(String[] args) {

Student a = new Student1("小明", "我读的书是 java", "我是大一学生");

Student b = new Student1("小明", "我读的书是  马列主义", "我是大二学生");

Student c = new Student1("小明", "我读的书是  javaee", "我是大三学生");

System.out.println(a);

System.out.println(b);

System.out.println(c);

}

}

/** 学生类 */

class Student {

private String name;

private String book;

private String type;

public Student(String name, String book, String type) {

this.name = name;

this.book = book;

this.type = type;

}

public String getName() {

return name;

}

public String getBook() {

return book;

}

public void setName(String name) {

this.name = name;

}

public void setBook(String book) {

this.book = book;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

@Override

public String toString() {

return "Student [name=" + name + ", book=" + book + ", type=" + type + "]";

}

}

/** 学生1 */

class Student1 extends Student {

public Student1(String name, String book, String type) {

super(name, book, type);

}

}

// /** 学生2 */

// class Student2 extends Student {

//

// public Student2(String name, String book) {

// super(name, book);

// }

//

// }

感觉没有类继承也能完成呢 不知道是不是这个意思 运行结果是 

Student [name=小明, book=我读的书是 java, type=我是大一学生]

Student [name=小明, book=我读的书是  马列主义, type=我是大二学生]

Student [name=小明, book=我读的书是  javaee, type=我是大三学生]

java怎么定义一个对象?

你应该问怎么实例化一个类生成一个对象

java 是先定义一个类 ,然后new 这个类,然后生成对象的

Java编程中类和对象的定义

1,什么是类?

答:类是客观存在的,抽象的,概念的东西。

2,什么事对象?

答:对象是具体的,实际的,代表一个事物。例如:车是一个类,汽车,自行车就是他的对象。

关于类与对象的描述:类是对象的模版,对象是类的一个个体

3,Java中定义类的方法?

class 类名

用Java语法定义人类:

public class Person

{

}

4,对象的定义方法?

1,对象声明:类名 对象名;

2,对象创建 对象名 =  new 类名(); new作用:分配内存空间。

也可以合写为:类名 对象名 = new 类名();

5,例子:创建一个空调,并调用制冷。

//设计一个空调

package practice;

public class Kongtiao {

    //空调属性

    String name;

    String pinpai;

    String pishu;

    int tempeture;

    

    //定义制冷行为

    void cool()

    {

        tempeture--;

    }

    

    //定义加热行为

    void hot()

    {

        tempeture+=7;

    }

    //主函数

    public static void main(String[] args){

        Kongtiao kt = new Kongtiao();

        kt.name = "美的";

        kt.pishu = "2匹";

        kt.tempeture = 20;

        kt.pinpai = "志高";

        /**

        kt.cool();

        System.out.println("空调的温度是"+kt.tempeture);

        */

        

        kt.hot();

        System.out.println("空调的温度是"+kt.tempeture);

        

        

    }

}

java程序不会写

第一个:

package xingzhuang;

public class Shape {

// 定义长宽高

private int a;

private int b;

private int c;

// 定义半径

private double r;

//定义圆周率

public static final double PI=3.14;

//定义设置和获取长宽高,和半径的方法

public int getA() {

return a;

}

public void setA(int a) {

this.a = a;

}

public int getB() {

return b;

}

public void setB(int b) {

this.b = b;

}

public int getC() {

return c;

}

public void setC(int c) {

this.c = c;

}

public double getR() {

return r;

}

public void setR(double r) {

this.r = r;

}

//定义求长方形面积的方法

public int recArea(int a,int b){

return a*b;

}

//定义求正方形面积的方法

public int squArea(int a){

return a*a;

}

//定义求三角形面积的方法

public double tranArea(int a,int c){

return a*c/2;

}

//定义求圆面积的方法

public double cirArea(double r){

return PI*r*r;

}

}

package xingzhuang;

public class Cir extends Shape{

public static void main(String[] args) {

Cir cir=new Cir();

cir.setR(2);//设置半径

//输出面积

System.out.println(cir.cirArea(cir.getR()));

}

}

package xingzhuang;

public class Rec extends Shape{

public static void main(String[] args) {

Rec rec=new Rec();

rec.setA(10);//设置长

rec.setB(10);//设置宽

//输出面积

System.out.println(rec.recArea(rec.getA(), rec.getB()));

}

}

package xingzhuang;

public class Squ extends Shape{

public static void main(String[] args) {

Squ squ=new Squ();

squ.setA(10);//设置长

//输出面积

System.out.println(squ.squArea(squ.getA()));

}

}

package xingzhuang;

public class Tran extends Shape{

public static void main(String[] args) {

Tran tran=new Tran();

tran.setA(10);//设置长

tran.setC(10);//设置宽

//输出面积

System.out.println(tran.tranArea(tran.getA(), tran.getC()));

}

}

第二个:

package dongwu;

public class Pet {

private String name;

private String type;

private double weight;

private double height;

private String sounds;

public String getSounds() {

return sounds;

}

public void setSounds(String sounds) {

this.sounds = sounds;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public double getWeight() {

return weight;

}

public void setWeight(double weight) {

this.weight = weight;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

@Override

public String toString() {

return " [宠物的名字=" + name + ", 种类=" + type + ", 重量=" + weight

+ ", 身高=" + height + ", 叫声=" + sounds + "]";

}

}

package dongwu;

public class Cat extends Pet{

public static void main(String[] args) {

Cat cat=new Cat();

cat.setName("cat");

cat.setType("猫科");

cat.setWeight(20);

cat.setSounds("喵");

cat.setHeight(10);

System.out.println(cat.toString());

}

}

package dongwu;

public class Dog extends Pet{

public static void main(String[] args) {

Dog dog=new Dog();

dog.setName("dog");

dog.setType("犬科");

dog.setWeight(20);

dog.setSounds("汪");

dog.setHeight(10);

System.out.println(dog.toString());

}

}

package dongwu;

public class Fish extends Pet{

public static void main(String[] args) {

Fish fish=new Fish();

fish.setName("fish");

fish.setType("鱼科");

fish.setWeight(20);

fish.setSounds("不叫");

fish.setHeight(10);

System.out.println(fish.toString());

}

}

package dongwu;

public class Frog extends Pet{

public static void main(String[] args) {

Frog frog=new Frog();

frog.setName("frog");

frog.setType("两栖科");

frog.setWeight(20);

frog.setSounds("呱呱");

frog.setHeight(10);

System.out.println(frog.toString());

}

}

你可以分别复制运行一下,看看是不是你想要的结果,希望能帮到你。

Java 注解的读取注解信息的方法

属于重点,在系统中用到注解权限时非常有用,可以精确控制权限的粒度

注意:要想使用反射去读取注解,必须将Retention的值选为Runtime Java代码import java.lang.annotation.Annotation;import java.lang.reflect.Method;//读取注解信息public class ReadAnnotationInfoTest {    public static void main(String[] args) throws Exception {        // 测试AnnotationTest类,得到此类的类对象        Class c = Class.forName(com.iwtxokhtd.annotation.AnnotationTest);        // 获取该类所有声明的方法        Method[] methods = c.getDeclaredMethods();        // 声明注解集合        Annotation[] annotations;        // 遍历所有的方法得到各方法上面的注解信息        for (Method method : methods) {            // 获取每个方法上面所声明的所有注解信息            annotations = method.getDeclaredAnnotations();            // 再遍历所有的注解,打印其基本信息            System.out.println(method.getName());            for (Annotation an : annotations) {                System.out.println(方法名为: + method.getName() + 其上面的注解为: + an.annotationType().getSimpleName());                Method[] meths = an.annotationType().getDeclaredMethods();                // 遍历每个注解的所有变量                for (Method meth : meths) {                    System.out.println(注解的变量名为: + meth.getName());                }            }        }    }}

javasettype的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、javasettype的信息别忘了在本站进行查找喔。