×

用php定义一个person类

用php定义一个person类(php定义一个常量)

admin admin 发表于2023-03-24 04:03:09 浏览57 评论0

抢沙发发表评论

本文目录一览:

定义一个Person类、一个Student子类和一个Professor子类

class Person{

private String name;

private int age;

Person(){}

Person(String name,int age){

this.name=name;

this.age=age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public void print(){

System.out.println(this.name+"\t"+this.age);

}

}

class Student extends Person{

private int score;

Student(){}

Student(int score){

this.score=score;

}

public int getScore() {

return score;

}

public void setScore(int score) {

this.score = score;

}

public void print(){

System.out.println("name:"+this.getName()+"\t"+"age:"+this.getAge()+"\t"+"score:"+this.score);

}

}

class Professor extends Person{

private String title;

public Professor() {

super();

}

public Professor(String title) {

super();

this.title = title;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public void print() {

System.out.println("name:"+this.getName()+"\t"+"age:"+this.getAge()+"\t"+"title:"+this.title);

}

}

public class Anyone {

public static void main(String[] args) {

Person s=new Student(100);

s.setName("zhangsan");

s.setAge(20);

s.print();

Person p=new Professor("ph");

p.setName("lisi");

p.setAge(40);

p.print();

}

}

定义一个Person类,包含私有属性姓名和性别,同时定义相应的构造函数

java是吧

/*****/

class Person {

private String name;

private char sex;

Person(String name,char sex) {

this.name = name;

this.sex = sex;

}

public String toString() {

return "name : " + this.name + "sex = " + this.sex;

}

public String GetName() {

return this.name;

}

public char GetSex() {

return this.sex;

}

public int hashCode() {

return name.hashCode();

}

public boolean equals(Object obj) {

Person p = (Person)obj;

if(this.name.equals(p.name) this.sex == p.sex)

return true;

return false;

}

};

/*****/

声明一个Person类,有name(String类型)、age(int类型)、sex(char类型

兄弟,你先说用什么语言啊

class Person{

public $name;

public $age;

public $sex;

public function __construct($name,$age,$sex){

$this-name = $name;

$this-age = $age;

$this-sex = $sex;

}

public function show(){

return $this-name.' '.$this-sex.' '.$this-age;

}

}

$person = new Person('令狐冲',21,'男');

echo $person-show();

这是php语言写的

定义一个Person类,它包括:●3个成员变量:姓名:name,字符串类型:String性别:se?

你好,很高兴回答你的问题。

实现代码如下:

public Person{

private String name;

private char sex;

private int age;

public Person(){}

public Person(String s){

this.name=s;

}

public String toString(){

return "姓名:"+name+",年龄:"+age+",性别:"+sex;

}

}

如果有帮助到你,请点击采纳。

我解答的大部分软件开发新人遇到的问题,如果有兴趣可以关注我。

用php写程序:定义个person类,属性(姓名,性别,年龄),方法(构造,说话,跑步,析构),在定义一

class Person{

private $name;

private $gender;

private $age;

function __construct($name,$gender,$age){

$this-name = $name;

$this-gender = $gender;

$this-age = $age;

}

function _say(){

return "_say in Person class";

}

function _run(){

return "_run in Person class";

}

function __destruct(){

}

}

class Student extends Person{

function say(){

return Person::_say()." and _say rewrite in Student class ";

}

}

$student = new Student("John","male","20");

echo $student-_run();

echo "br/";

echo $student-say();

创建一个类Person,对该类的要求如下

class Person{

private String name;

protected char sex;

public int age;

public Person() {

// TODO Auto-generated constructor stub

}

public Person(String name) {

this.name = name;

}

public Person(String name,char sex, int age){

this(name);

this.sex = sex;

this.age = age;

}

}