会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133872个问题

老师,在29-34行中,使用接口Animal类来获取Mouse,Bird的run,fly获取不了,视频里老师也没有进行这2个参数的演示。

from flask import Flask
from flask_graphql import GraphQLView
import graphene

class Animal(graphene.Interface):  #定义一个接口类
    id=graphene.ID()
    name=graphene.String()

class Mouse(graphene.ObjectType):
    class Meta:
        interfaces=(Animal,)    #继承接口中的属性
    run = graphene.String()     #自定义属性

class Bird(graphene.ObjectType):
    class Meta:
        interfaces = (Animal,)  #继承接口中的属性
    fly = graphene.String()     #自定义属性

class Query(graphene.ObjectType):
    mouse=graphene.Field(Mouse) #获得Mouse属性
    bird=graphene.Field(Bird)   #获得Bird属性
    animal=graphene.Field(Animal,type_=graphene.Int(required = True))

    def resolve_mouse(self,info):
        return Mouse(id=1,name='杰瑞',run='汤姆快跑呀~~')
    def resolve_bird(self,info):
        return Bird(id=2,name='高书博',fly='汤姆快跑呀~~')
    # def resolve_animal(self,info,type_):
    #     if type_==1:
    #         return Mouse(id=3, name='狗蛋高书博', run='追唐老鸭啦~~~')
    #     elif type_==2:
    #         return Bird(id=4, name='狗子高书博', fly='跑呀~Tom来了!!')

    def resolve_animal(self,info,type_):
        # return Animal(id=3,name='动物') #  "message": "An Interface cannot be intitialized",
        if type_ == 1:
            return Mouse(id= 4, name='米老鼠',run='追唐老鸭啦~~~')
        else:
            return Bird(id = 3,name='鹦鹉',fly = '跑呀~Tom来了!!')

if __name__=='__main__':
    schema=graphene.Schema(query=Query)  #查询Query
    app=Flask(__name__) #创建flask对象
    # /graphql:视图的URL, GraphQLView.as_view:将映射函数转换成视图函数 ,'grapql':视图名称  ,
    # 把schema注册到schema方法里, graphiql=True:开启插件应用
    app.add_url_rule('/graphql', view_func=GraphQLView.as_view('grapql', schema=schema, graphiql=True))
    app.run()

image.png

Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/GraphQL 16292楼

User 类:

public class User {

    private String userName;
    private int userAge;
    public String location;

    public User() {
    }

    public User(String userName, int userAge, String location) {
        this.userName = userName;
        this.userAge = userAge;
        this.location = location;
    }

    // private modifier
    private User(int userAge) {
        this.userAge = userAge;
    }

     /*only the name is different, can not constitute an overload,
     so it can not add the constructor that only have a location parameter.*/
    public  User(String userName) {
        this.userName = userName;
    }

    public User(String userName, String location) {
        this.userName = userName;
        this.location = location;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getUserAge() {
        return userAge;
    }

    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public void testMethod () {
        System.out.println("hello!");
    }

    private void testWay () {
        System.out.println("overload");
    }

    public void testWay (int id) {
        System.out.println(id);
    }

    @Override
    public String toString() {
        /* Considering code writing from the bottom level, in order to cope with different functional requirements */
        return "User{" +
                "userName='" + this.getUserName()+ '\'' +
                ", userAge=" + this.getUserAge()+
                ", location='" + this.getLocation() + '\'' +
                '}';
    }
}

通过反射调用方法测试类:

public class GetMethodTest02 {

    public static void main(String[] args) {
        User user01 = new User();
        Class classObject01 = user01.getClass();

        Class classObject02 = User.class;

        Class classObject03 = null;
        try {
            classObject03 = Class.forName("com.bjsxt.demo.User");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        /* call the public method */
        Method method01 = null;
        try {
            method01 = classObject03.getDeclaredMethod("testWay",int.class);   // get designed method object.
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        Object user02 = null;
        try {
            user02 = classObject03.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        try {
            method01.invoke(user02,3);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        // how call the private method?

    }
}

老师,在调用方法的测试类中,如何调用自定义的用 private 修饰的 testWay() 方法?还是说通过反射只能调用类的 public 方法,不能调用 pirvate 的方法.

JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术(旧) 16293楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 16294楼

ego_parent.zip

老师,是因为跨域的原因吗?image.png

JAVA 全系列/第十八阶段:亿级高并发电商项目_架构/编码(旧)/电商:使用SpringCache+Redis实现门户导航缓存 16295楼
Python 全系列/第十二阶段:Python_Django3框架/Django初级 16296楼
JAVA 全系列/第一阶段:AI驱动的JAVA编程/控制语句、方法、递归算法 16297楼
Python 全系列/第十九阶段:数据分析-数据可视化/seaborn 16299楼

'''创建一个经典的GUI程序的写法,使用面向对象的方法'''
from tkinter import *
import webbrowser
class Application(Frame):
    '''一个经典的GUI写法'''
    def __init__(self,master=None):
        super().__init__(master)
        self.master=master
        self.pack()
        self.creatWidget()
    def creatWidget(self):
        '''创建组件'''
        self.w1=Text(root,width=20,height=12,bg="gray")
        self.w1.pack()
        self.w1.insert(1.0,"0123456789\nabcdefg")
        self.w1.insert(2.3,"锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦\n")

        Button(self,text="重复插入文本",command=self.insertText).pack(side="left")
        Button(self,text="返回文本",command=self.returnText).pack(side="left")
        Button(self,text="添加组件",command=self.addWidget).pack(side="left")
        Button(self,text="添加图片",command=self.addImage).pack(side="left")
        Button(self,text="通过tag精确控制文本",command=self.testTag).pack(seid="left")




        def insertText(self):
            self.w1.insert(INSERT,'Wsq')
            self.w1.insert(END,'[sxt]')
            self.w1.insert(1.8,'wsq')
        def returnText(self):
            print(self.w1.get(1.2,1.6))
            print("所有文本内容:\n"+self.w1.get(1.0,END))
        def addWidget(self):
            b1=Button(self.w1,text='尚学堂')
            self.w1.window_create(INSERT,window=b1)
        def addImage(self):
            self.photo=PhotoImage(file="b/asd.gif")
            self.w1.image_create(END,image=self.photo)
        def testTag(self):
            self.w1.delete(1.0,END)
            self.w1.insert(INSERT,"good good study,day day up!\n北京尚学堂\n百战程序员\n百度,搜索一下")
            self.w1.tag_add("good",1.0,1.9)
            self.w1.tag_config("good",background="yellow",foreground="red")
            self.w1.tag_bind("baidu","<Button-1>",self.webshow)





root=Tk()
root.geometry("400x200+200+300")
root.title("一个经典的GUI程序的测试")

app=Application(master=root)
root.mainloop()

GIF.gif

老师这个问题出在哪了?一直找不出来...

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 16301楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 16303楼
Python 全系列/第九阶段:Flask百战电商后台系统/Flask百战电商后台项目 16304楼
Python 全系列/第一阶段:AI驱动的Python编程/序列 16305楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637